some basic questions...

David Stockwell winexpert at hotmail.com
Mon Sep 20 18:20:40 CEST 2004


Hi Player,

For the second part,

I tend to think of the self as a reference to an object thats sitting on a 
stack somewhere.  With python you don't have to worry about the stack at 
all, instead you just use the 'self' to navigate the stack.

in your example, it would be done this way
if _name ==  '_main_':
         myGame = Application()
         myGame.run()


the object 'myGame' becomes what self refers to when run wants to do 
something with 'self' (You write the code, etc).

Instantiating Application places that object on the stack.  Then the rest of 
your program is relative to that stack object.

I think you could have also done:

if _name_ == '_main_':
     Application().run()

But I've never done that.

--------------------------------------



David
-------
Surf a wave to the future with a free tracfone 
http://cellphone.duneram.com/index.html




>From: "Player" <guess at My.email.address.scum.com>
>To: python-list at python.org
>Subject: some basic questions...
>Date: Mon, 20 Sep 2004 17:05:13 +0100
>
>-----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-----
>
>
>--
>http://mail.python.org/mailman/listinfo/python-list

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/




More information about the Python-list mailing list