Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > some basic questions...

Reply
Thread Tools

some basic questions...

 
 
Player
Guest
Posts: n/a
 
      09-20-2004

-----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-----


 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      09-20-2004
"Player" <(E-Mail Removed)> wrote in message
news:cimv3o$a51$(E-Mail Removed)...
>
> [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


--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.


 
Reply With Quote
 
 
 
 
Gerrit
Guest
Posts: n/a
 
      09-20-2004
Player wrote:
> [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?

>>> import foo

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/progra...nt-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:

>>> class Foo(object):

.... def method(self):
.... return self
....
>>> f = Foo()
>>> f.method() == f

True


See also:
http://www.python.org/doc/faq/progra...l#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.

--
Weather in Twenthe, Netherlands 20/09 18:25:
13.0°C wind 8.9 m/s SSW (57 m above NAP)
--
Gerrit Holl - 2nd year student of Applied Physics, Twente University, NL.
Experiences with Asperger's Syndrome:
EN http://topjaklont.student.utwente.nl/english/
NL http://topjaklont.student.utwente.nl/
 
Reply With Quote
 
marcus@deepfort.com
Guest
Posts: n/a
 
      09-22-2004

>[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

>>> __name__

'__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.

>>> import test
>>> test.__name__

'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 )

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM
Interactive tutorial of some kind for teaching people basic Windows/some internet browsing? fitwell Computer Support 10 09-19-2003 06:07 AM
can someone please tell me some Visual Basic websites for some help? valentin Computer Support 2 06-25-2003 05:33 PM



Advertisments