some basic questions...

Discussion in 'Python' started by Player, Sep 20, 2004.

  1. Player

    Player Guest

    -----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-----
     
    Player, Sep 20, 2004
    #1
    1. Advertising

  2. Player

    Russell Blau Guest

    "Player" <> wrote in message
    news:cimv3o$a51$...
    >
    > [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.
     
    Russell Blau, Sep 20, 2004
    #2
    1. Advertising

  3. Player

    Gerrit Guest

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

    >>> class Foo(object):

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

    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.

    --
    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/
     
    Gerrit, Sep 20, 2004
    #3
  4. Player

    Guest


    >[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 :) )
     
    , Sep 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Klett

    <div> tags, some basic questions

    Steve Klett, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    Kevin Spencer
    Nov 13, 2003
  2. Michael Hesse
    Replies:
    6
    Views:
    446
    Thomas Weidenfeller
    Jul 26, 2005
  3. ccs

    Some basic questions

    ccs, Jun 11, 2004, in forum: C++
    Replies:
    6
    Views:
    351
    Mike Wahler
    Jun 11, 2004
  4. slot

    Some basic questions

    slot, Aug 3, 2004, in forum: C++
    Replies:
    8
    Views:
    368
    Default User
    Aug 3, 2004
  5. jonathan.beckett

    Some basic newbie questions...

    jonathan.beckett, Dec 28, 2006, in forum: Python
    Replies:
    13
    Views:
    495
    Kent Johnson
    Jan 2, 2007
Loading...

Share This Page