Something in the function tutorial confused me.
Stargaming
stargaming at gmail.com
Mon Aug 6 08:00:25 EDT 2007
On Mon, 06 Aug 2007 11:13:45 +0000, Alex Popescu wrote:
> Stargaming <stargaming at gmail.com> wrote in news:46b6df49$0$26165
> $9b622d9e at news.freenet.de:
>
[snip]
>>
>> You're just unluckily shadowing the name `y` in the local scope of
> your
>> function. Your code snippet could be rewritten as::
>>
>> def f(x, y=None):
>> if y is None: my_y = []
>> else: my_y = y
>> my_y.append(x)
>> return my_y
>>
>> HTH,
>> Stargaming
>
> For the given example this will continue to print:
>
>> print f(23) # prints [23]
>> print f(42) # prints [42]
>
> so this doesn't solve/explain OP's initial question. A previous post has
> already clarified the reasons for seeing this normal behavior.
>
> bests,
> ./alex
If it keeps normal behaviour, that's exactly what it ought to explain. In
his local scope, there is an `y`, having the value of f.func_defaults.
Because it's harder to see "hey, in some cases, y vanishes, in some it
survives", I invented the new local reference `my_y` -- which should be
clear to go away after completion of the function body.
Regards,
Stargaming
More information about the Python-list
mailing list