Why this code is working?

Benjamin Kaplan benjamin.kaplan at case.edu
Wed Jan 14 08:04:04 EST 2009


On Wed, Jan 14, 2009 at 7:59 AM, Hussein B <hubaghdadi at gmail.com> wrote:

> On Jan 14, 2:21 pm, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:a
> > On Wed, 14 Jan 2009 01:57:48 -0800, Hussein B wrote:
> > >> Well... Because it is correct ?
> >
> > >> What make you think it _shouldn't_ work ?
> >
> > > Because def2 is defined after def1 in an interpreted language, not
> > > compiled.
> >
> > Python is compiled.
> >
> > What do you think the c in .pyc stands for? And what do you think the
> > compile() function does?
> >
> > It's just not compiled to machine code. It's compiled to byte code.
> >
> > --
> > Steven
>
> Yes I know Python programs can be compiled but when I do:
> python Script1.py
> Did Python compile Script1 into the memory?
>  --
>

yes it does, but not in the way you are thinking. Python compiles to its
byte code as needed, not at the start of the program. Because Python is
dynamic, you can change things on the fly. For instance, this code is
perfectly valid

def f2(x) :
   print x

def f1() :
   f2(88)

f1() #prints 88

def f2(x) :
   print x/2

f1() #prints 44

Because the code can change on the fly, Python looks for f2 every time f1 is
called, not when it first sees the function. This is the beauty of a dynamic
language.

>
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090114/f678a1c3/attachment-0001.html>


More information about the Python-list mailing list