Is this unpythonic?

Frank Millman frank at chagford.com
Sat May 9 01:56:54 EDT 2015


"Steven D'Aprano" <steve+comp.lang.python at pearwood.info> wrote in message 
news:554cd1190ドル12977ドル$c3e8da3$5496439d at news.astraweb.com...
> On Fri, 8 May 2015 08:53 pm, Frank Millman wrote:
>>>> Does z have to be a list? Could you use an empty tuple instead?
>>>>>> def x(y, z=()): ...
>>>>>>> That was Chris' suggestion as well (thanks Chris).
>>>> The idea appealed to me, but then I found a situation where I pass in a
>> dictionary instead of a list, so that would not work.
>>> Why wouldn't it work? If it worked with an empty list, it will probably 
> work
> with an empty tuple instead.
>
Sorry, I should have been more explicit. In the case of a dictionary, I used 
'def x(y, z={}'
I have not checked, but I assume that as dictionaries are mutable, this 
suffers from the same drawback as a default list.
Unlike a list, it cannot be replaced by an empty tuple without changing the 
body of the function.
Dave's suggestion would have worked here -
 EMPTY_LIST = []
 EMPTY_DICT = {}
But as I have decided to use the None trick, I use it for a default 
dictionary as well.
Frank


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /