mutable default parameter problem [Prothon]

Mark Hahn mark at prothon.org
Wed Jun 16 17:14:41 EDT 2004


Peter Hansen wrote:

>> In Prothon:
>>     def foo(x):
>>             print foo.list.append!(x)
>>     foo.list = []
>>
>> (Sorry. I couldn't resist bragging.)
>
> About what?
>
> Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
>  >>> def foo(x):
> ...   foo.list.append(x)
> ...   print foo.list
> ...
>  >>> foo.list = []
>  >>> foo('test')
> ['test']
>
> (Oh, did you mean bragging about how a hard-to-see exclamation
> mark causes append() to return the sequence?  I thought
> maybe it was about the function attribute or something.)

Actually, I didn't know if the function attribute assignment outside the
function would work in Python or not.  I guess I'll know better than to try
to play one-upmanship with Python next time.  I did say I was sorry :-)

FYI: It's not that the exclamation mark causes append to return the
sequence.  The exclamation mark is always there and the sequence is always
returned.  The exclamation mark is the universal symbol for in-place
modification.  This is straight from Ruby and solves the problem that caused
Guido to not allow sequences to be returned.  And, yes, I do think that's
worth bragging about ;-)





More information about the Python-list mailing list