some basic questions...

P

Player

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello

I am teaching myself python, and I have gotten a long way, it's quite a
decent language and the syntax is great :)

However I am having a few, "problems" shall we say with certain conventions
in python.

In the book I am using to teach me more of the inner working of python, and
further into python that the basic tutorials go, there is this wording
below...

[book quote]
When a file containing python code is executed, the built in variable _name_
is populated with the name of the module being executed. If the value of
_name_ is _main_, then that file is the original file that was used to
invoke the application from command line or an icon.
This is usefull, as it allows code to know the difference between when it is
invoked & when it is imported by another python program. It is also provides
a convenient place to provide one-time startup code.
[end quote]

Can someone explain this in some different wording, because I dn't know if
my understanding of what is said in that paragraph is right or not?

ALSO
Below is some example of some code, for use with python and PyGame and PyUI,
that I would like to ask a question about..

import pyui

class Application:
def _init_(self, width, height):
self.width = width
self.height = height

def run(self):
"""I am called to begin the Application or game.
"""

def run()
width = 800
height = 600
pyui.init(width, height)
app= Application(width, height)
app.run()

if _name == '_main_':
run()

Now I no that this code declares an Application class, and then invokes a
run method to create an instance of that class. Then the Application cobject
then uses the run method called to start the main loop.

Which basically created a game window of size 800 by 600.

What I don't understand is the, "self" bit of the two top functions.

Can somebody explain what the, "self" is actually for??

Thanks ina dvance :)

Player




- --
*************
The Imagination may be compared to Adam's dream-
he awoke and found it truth.
John Keats.
*************
-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQU7/ty/z2sM4qf2WEQJRrQCguAtRwDQ6UfFRXDGZ63DDjWdnID0AmwX3
4K3iWiybh9BEKPh9b2h0m9Tr
=0niY
-----END PGP SIGNATURE-----
 
R

Russell Blau

Player said:
[book quote]
When a file containing python code is executed, the built in variable _name_
is populated with the name of the module being executed. If the value of
_name_ is _main_, then that file is the original file that was used to
invoke the application from command line or an icon.
This is usefull, as it allows code to know the difference between when it is
invoked & when it is imported by another python program. It is also provides
a convenient place to provide one-time startup code.
[end quote]

Actually it is '__name__' and '__main__', with *two* _ characters both
before and after. You need to be careful about the underscores when using
built-in names like these, because typing them with just one _ will not work
at all!

As for how to use __name__ and the importing modules question, I suggest you
read http://www.python.org/doc/current/tut/node8.html
 
G

Gerrit

Player said:
[book quote]

Can someone explain this in some different wording, because I dn't know if
my understanding of what is said in that paragraph is right or not?

A .py file can be executed in several ways. When you use 'import foo',
the value of 'foo.__name__' is "foo". When you start foo.py directly,
the value of __name__ is '__main__':

Suppose the content of foo.py is:

print __name__

Now let's run it from the commandline:

$ python foo.py
__main__

What happens when we import it as a module?
foo

The same code gets executed, but with a different result.
So, the special variable '__main__' gives us a way to tell the
difference: are we being executed "directly by the user", or are we
being used as a library? In many occasions, we want to act differently.
What is seen often is this:

if __name__ == '__main__':
main()

This executed the main() function if and only if the .py file is being
run directly from the commandline or from Windows. If we import the
file, we may not want to run the program at all: the if-block prevents
it.

See also:
http://www.python.org/doc/faq/programming.html#how-do-i-find-the-current-module-name
:

A module can find out its own module name by looking at the predefined
global variable __name__. If this has the value '__main__', the program
is running as a script. Many modules that are usually used by importing
them also provide a command-line interface or a self-test, and only
execute this code after checking __name__:

def main():
print 'Running test...'
...

if __name__ == '__main__':
main()
Can somebody explain what the, "self" is actually for??

Well, that's a long story, it's all about Object Oriented Programming.
In short, 'self' is, well, self:
.... def method(self):
.... return self
....True


See also:
http://www.python.org/doc/faq/programming.html#what-is-self :

Self is merely a conventional name for the first argument of a method. A
method defined as meth(self, a, b, c) should be called as x.meth(a, b,
c) for some instance x of the class in which the definition occurs; the
called method will think it is called as meth(x, a, b, c).

hope this helps,
Gerrit Holl.
 
M

marcus

[book quote]
When a file containing python code is executed, the built in variable _name_
is populated with the name of the module being executed. If the value of
_name_ is _main_, then that file is the original file that was used to
invoke the application from command line or an icon.
This is usefull, as it allows code to know the difference between when it is
invoked & when it is imported by another python program. It is also provides
a convenient place to provide one-time startup code.
[end quote]


Can someone explain this in some different wording, because I dn't know if
my understanding of what is said in that paragraph is right or not?

*When you import a python module, the value in that module's __name__
attribute will be its own name.

When you "run" the same python module, the value in that module's
__name__ attribute will be "__main__"*

That means, simply, you can differentiate between the module that is
being explicitly run, say from a shell, and the module that is imported,
by looking at the particular module's __name__ attribute.

You can demonstrate this to yourself thus:

1. run a python file(module) in interactive mode and look at the value
of __name__

you@host$ python -i test.py
'__main__'

2. in a new python shell, import the same python module and look at the
value of module.__name__

you@host$ python

Python 2.3.4 (May 29 2004, 19:23:07)
[GCC(ellipsis)] on HesnottheMessiahhesaverynaughtyboy
Type "help", "copyright", "credits" or "license" for more information.
'test'

I hope that demonstrates it, and yes, it's probably a terrible example
(use a module which exists in your path for the second one :) )

So say where you had:

import pyui
<snip>
if __name__ == '__main__':
x = Application(width, height)
x.run()

if __name__ == '__main__': - means "only run the next bit of code if
this module is explicitly run, and ignore it if this module is imported."

I've probably explained it really, really badly.

Good to see another mere mortal on the list though :)


Can somebody explain what the, "self" is actually for??
The self points back to the instance you will create at runtime. The
best thing I can suggest for that is to get used to using it, as it will
make more and more sense to you as you go on. (Some would growl that's
debatable - not me though :) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top