Nested scopes: design or implementation?

logistix logstx at bellatlantic.net
Wed Mar 6 17:10:53 EST 2002


Okay, now I'm confused.

I was perfectly happy with Teddy Reedy's explaination that def is an
expression and not a declaration.

But if I'm following you, the embedded attribute function isn't being
recompiled to bytecode each time function() is called.  If the code object
is only getting created once, then why are the defaults getting
recalculated?

--
-

<mmillikan at vfa.com> wrote in message news:3czdq05v.fsf at vfa.com...
> Note that the <func_code> object is shared by each of the inner
> <function> objects:
>
> text = "random text"
> def function():
>     def attribute( bindNow = text):
>         print bindNow
>     function.attribute = attribute
>
> Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
>
> >>> function()
> >>> function.attribute()
> random text
> >>> function.attribute
> <function attribute at 01A7930C>
> >>> function.attribute.func_code
> <code object attribute at 01A79F48, file "c:\\python-YYAS0o", line 3>
>
> >>> function()
> >>> function.attribute.func_code
> <code object attribute at 01A79F48, file "c:\python-YYAS0o", line 3>
>
> >>> text = 'new random text'
> >>> function()
> >>> function.attribute
> <function attribute at 01A450CC>
> >>> function.attribute.func_code
> <code object attribute at 01A79F48, file "c:\python-YYAS0o", line 3>
>
> Mark Millikan





More information about the Python-list mailing list