David Kaplan wrote:
>> * Using an empty list in a python kwarg as the default is a gotcha, as in
>>
>>     def calc_label_rot_and_inline( self, slc, ind, lw, lc=[], spacing=5 ):
>>
>> The reason is that if the function mutates the list, this often leads
>> to unintended consequences as the list is module level and thus shared
>> between instances and method calls.  The standard idiom for mutable
>> (lists and dicts) keyword args s
>>
>>     def func(x=None):
>>         if x is None: x = []
>>
>> I have fixed this in contour.py
>>
> 
> I don't really understand how this can be a problem, but it probably
> isn't that important unless you feel like enlightening me.

The default value for the function is created when the interpreter 
executes the define statement.  Thus, if you were to do something that 
modifies the list inplace, like append(), the default argument will 
retain those changes.  For instance, try this:

def foo(l=[]):
     l.append('foo')
     print l
foo()
foo()
foo(['bar'])
foo()

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to