Simple Python class questions

Duncan Booth duncan.booth at invalid.invalid
Fri Jun 20 08:07:18 EDT 2008


John Dann <news at prodata.co.uk> wrote:

> Yes I was wondering about that, but I wasn't clear about when 'body'
> code (ie not contained within a def block) in the module might run
> under Python. So it seemed to be safer to place the import statement
> inside the 'constructor' to get the earliest warning of non-visibility
> of pyserial. But you seem to be implying that the body code will run
> when the class is instantiated - have I understood that right? It
> surely doesn't run when the module containing the class is imported
> into the main module - does it?? It would certainly make life easier
> to place the import in the body of the module.

Python starts executing at the top of your main script and then proceeds 
line by line down until it falls off the bottom. Various things can 
divert it from this straightforward progression towards the end of the 
script, some of them such as if/for/while/raise or function calls are 
obvious, but the less obvious ones include:

import somemodule (or 'from somemodule import something')

if 'somemodule' has not previously been imported this will find the 
module and execute the lines of code in the module from top to bottom 
just as for the main script. When it falls off the bottom of the module 
it returns to the import statement, assigns the module or the imported 
attributes to a name or names in the calling namespace (yes, an import 
is just a highly specialised assignment statement), and then continues 
with the next statement.

If somemodule has already started being imported anywhere in the program 
then the import simply returns immediately and does the assignment. 
(This can be a cause of confusion if you try to import modules 
recursively as it is perfectly possible that the imported module has not 
yet finished executing, so it may not yet have all the classes and 
functions you expect).

class somename(bases):
    somecode

A 'class' statement in Python is just executable code. The body of the 
class is executed from top to bottom but in a new namespace. When 
execution falls off the bottom of the class body a new class object is 
created from that namespace and the base classes. The new class object 
is then assigned to 'somename' (i.e. a class statement is a specialised 
assignment statement). Then execution then procedes with the next 
statement.

def functionname(arg1, arg2=default):
    somecode

A 'def' statement in Python is also a specialised assignment statement: 
unlike 'class' it doesn't immediately execute the code in the body, but 
it does evaluate any default argument values when it encounters the 
'def'. Then it creates a new function object from the code in the body 
and the default arguments (and a few other things such as the argument 
specification and the function name). Then it continues with the next 
statement.

global name

The 'global' statement is not executed at runtime. It is the only Python 
statement which is purely compile time.

Once you understand this it should be much clearer: everything except 
global is executed when it is encountered following the normal rules for 
program flow, and all the ways of creating module, classes, and 
functions simply execute some code and then do an assignment (and so if 
you wish you can later overwrite the values they assigned).

If you wish to do something special when an import fails then you simply 
put try:..except: around the import at the top level in a module and 
handle it there: you don't need to put either the import or the handler 
inside a function.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list