[Python-ideas] func attribute & default arg

Terry Reedy tjreedy at udel.edu
Thu May 14 23:33:36 CEST 2009


spir wrote:

> To sum up, I think the core of the issue is not about static/dymamic
> evaluation, or compile time/run time; rather it lies in the fact that
> python internally does not separate a default *value* from the
> corresponding local *variable*.

Good try, but you seem to have missed the basic point that underlies at 
several ways that newbies get tripped up*. Python is an *object* based 
language, not a *value* based language.  By 'value', I mean the 
information carried by the object.  Objects have an identity, values do 
not.  This is a defining characteristic of Python.  If one wants an 
immutable value language, Python is not the right choice.

For immutable objects, the difference is nearly invisible.  For mutable 
objects, which generally do not exist in mathematics (which is generally 
timeless) the difference is crucial to understand.  When people are 
tripped up by their 'intuition', it is sometimes intuition based on math 
that does not apply to mutable objects.  Names are bound to objects, not 
to values.

For functions, parameters are local names, arguments are objects (and 
not values).  Functions may have default objects, not 'default values' 
(as you say above and elsewhere).  If a default object is mutable, it 
has an initial value, but not a constant value.  Function objects 
already have an attribute that is a tuple of default objects. 
(Parameter names, without and with defaults, are part of the code object.)

Python 3.0.1 ,,, on win32
 >>> def f(a, b=1, c='1'): pass

 >>> f.__defaults__
(1, '1')

Terry Jan Reedy

* Mutable objects with multiple names, mutable objects passed as 
argument, mutables used as defaults, constructing initialized lists of 
lists.




More information about the Python-ideas mailing list