Definition statement

Alex Martelli aleaxit at yahoo.com
Fri Aug 17 03:30:25 EDT 2001


"Patio87" <patio87 at aol.com> wrote in message
news:20010817030426.14357.00003436 at mb-mi.aol.com...
> Im am learning python right know and I have come to a chapter called
functions
> and procedures. It goes into detail about the "def" function. I canoont
seem to

def is not a function, but, as you say in the subject, a statement.  It
*defines*
a function.

> understand what it does. can someone please tell me what it means. If it
is any

When the interpreter executes the statement 'def', it builds a function
object
from the parameters of the def, and binds it to the name that is the first
parameter to the statement 'def'.  Later, other parts of your code may use
the call operation to execute the code of that function object.

Consider a simple example:

def xx(): print 'yy'

The Python compiler will turn this into the bytecode instructions for:
    xx = make a new function with: name='xx', 0 arguments, and a suitable
code-object
the 'suitable code object' being a constant (generated by the compiler) with
the bytecode instructions for "print the constant string 'yy' then a newline
and return None".


Alex






More information about the Python-list mailing list