Is this unpythonic?

Johannes Bauer dfnsonfsduifb at gmx.de
Sun May 10 04:04:48 EDT 2015


On 08.05.2015 14:04, Dave Angel wrote:

> It might be appropriate to define the list at top-level, as
> 
> EMPTY_LIST=[]
> 
> and in your default argument as
>     def x(y, z=EMPTY_LIST):
> 
> and with the all-caps, you're thereby promising that nobody will modify
> that list.
> 
> (I'd tend to do the None trick, but I think this alternative would be
> acceptable)

I think it's a really bad idea to use a module-global mutable
"EMPTY_LIST". It's much too easy this happens:

# Globally
>>> EMPTY_LIST = [ ]

# At somewhere in the code at some point in time
>>> foo = EMPTY_LIST
>>> foo.append(123)
>>> print(foo)
[123]

# Some other place in code
>>> bar = EMPTY_LIST
>>> print(bar)
[123]

Regards,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1 at speranza.aioe.org>



More information about the Python-list mailing list