functions, optional parameters

Gregory Ewing greg.ewing at canterbury.ac.nz
Sat May 9 03:27:56 EDT 2015


Chris Angelico wrote:

> So no, it
> isn't proof - it's equally well explained by the code object being
> constant.

I suppose, strictly speaking, that's true -- but
then the code object *might as well* be created
at compile time, since the semantics are identical.

In any case, it's easy to see from the data structure
that the default values are kept in the function object:

Python 3.4.2 (default, Feb  4 2015, 20:08:25)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> y = "spam"
 >>> def f(x = y): pass
...
 >>> f.__defaults__
('spam',)

I suppose you could argue that f.__defaults__
could be a computed property that's looking inside
f.__code__ somewhere, but that would be stretching
credibility.

-- 
Greg



More information about the Python-list mailing list