473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

some basic questions...


-----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-----
Jul 18 '05 #1
3 1509
"Player" <gu***@My.email.address.scum.com> wrote in message
news:ci**********@newsg2.svr.pol.co.uk...

[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.
Jul 18 '05 #2
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/
Jul 18 '05 #3
[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 :-) )

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
2
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
7
by: Steven O. | last post by:
I am basically a hobbyist programmer, at the moment doing a little work experimenting with some AI stuff. I learned C++, and then tried to teach myself MFC using MS Visual C++ 6.0. I swore off of...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
1
by: danilo of TUP | last post by:
Greetings, im Danilo, a student from TUP Manila and taking up COMPUTER SCIENCE. we are going to have a defense or we could say a project in turbo c. i just wanna know how to use the SWITCH...
3
by: aziz001DETESTSPAM | last post by:
I've been following some tutorials and reading books and have a basic grasp of the fundamental ADO.NET commands. But all the books I've read use a database with only 1 table. My database has many,...
2
by: Water Cooler v2 | last post by:
I've not touched SQL server programming since 1999. I have very little memory of it and need some clarifications on some basic questions that I could even use a book for. Until I get myself a good...
0
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
3
by: alcapoontje | last post by:
hi there, i'm already searching the internet for a couple of days to find an answer on this (simple) questions but i couln't find anything usefull. - how to quit and open a programm like "word" with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.