some basic questions...

David Stockwell winexpert at
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()

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_':

But I've never done that.


Surf a wave to the future with a free tracfone

>From: "Player" <guess at>
>To: python-list at
>Subject: some basic questions...
>Date: Mon, 20 Sep 2004 17:05:13 +0100
>Hash: SHA1
>  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
>[book quote]
>When a file containing python code is executed, the built in variable 
>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 
>invoked & when it is imported by another python program. It is also 
>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?
>Below is some example of some code, for use with python and PyGame and 
>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)
>     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 
>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 :)
>- --
>The Imagination may be compared to Adam's dream-
>he awoke and found it truth.
>John Keats.
>Version: PGP 8.0

FREE pop-up blocking with the new MSN Toolbar – get it now!

More information about the Python-list mailing list