How does python work

Michael Chermside mcherm at mcherm.com
Thu Jul 10 16:00:51 CEST 2003


First, let me answer your questions:

Geiregat Jonas writes:
> I'm asking myself how Python code get's executed ...

Normally, a compiler compiles the code to a .pyc file, then an interpreter
processes the .pyc file executing the code therein.

> What happens first etc ...
> First read the file and then ..

The script you provide is executed statement-by-statement in order from
top to bottom. Of course, if a statement (eg: import) involves executing
code from another file or from some C extension, then that happens when
the invoking statement is processed. Most programs put a bunch of imports
then a bunch of class and function definitions, then a small bit of
actual code-to-be-executed at the bottom, after a little incantation
like this:  "if __name__ == '__main__':".

> How python handle's variable internally ...

In all cases, the actual object is allocated on the heap. A variable
is a reference to an object, and they are stored different places
depending on the scope of the variable. Global (ie module-level) 
variables are stored in a hash table (dict). Object instance variables 
are stored in a dict. Class-level variables like methods are stored in 
a dict. Local function variables are stored on the stack but made to 
LOOK like they're in a dict.

> also could someone point me to the build in functions in the python source
> code ?

Pointing you to parts of the Python code is beyond the scope of this
posting.


Anyway, now that I've answered the questions you've ASKED (except the last
one), let me answer the questions you DIDN'T ask but might be wise to ask:

QUESTION: Where can I go to see a beginners tutorial on Python?
          There are quite a few. A list can be found here:
          http://www.python.org/doc/Newbies.html

QUESTION: Where can I find Python's documentation?
  ANSWER: http://www.python.org/doc/
          I would start by reading the Tutorial... it's an excellent
          introduction to Python. Then you'll probably want to start
          browsing through bits of the Library Reference.

QUESTION: Where can I ask questions about Python?
  ANSWER: This newsgroup is fine, but you might also want to check out
          the Python Tutor List, found at 
          http://mail.python.org/mailman/listinfo/tutor

QUESTION: Can I learn Python just by experimenting with it?
  ANSWER: For the most part, YES! I suggest installing Python, then
          finding a way to run it from the command line (how you do
          that depends on your operating system). Then play around
          a bit. Of course, you'll still want the documentation
          available, but you can figure out lots just by doing this.

-- Michael Chermside


-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/





More information about the Python-list mailing list