Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90214

Re: Is this unpythonic?

From "Frank Millman" <frank@chagford.com>
Subject Re: Is this unpythonic?
Date 2015-05-09 08:51 +0200
References <mailman.224.1431072121.12865.python-list@python.org> <554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com> <mailman.231.1431082398.12865.python-list@python.org> <554cd119$0$12977$c3e8da3$5496439d@news.astraweb.com> <mik7j6$59n$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.273.1431154310.12865.python-list@python.org> (permalink)

Show all headers | View raw


"Frank Millman" <frank@chagford.com> wrote in message 
news:mik7j6$59n$1@ger.gmane.org...
>
> "Steven D'Aprano" <steve+comp.lang.python@pearwood.info> wrote in message 
> news:554cd119$0$12977$c3e8da3$5496439d@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.
>

Cough, cough, I really should have given that a moment's thought before 
posting.

It just dawned on me that a dictionary *can* be replaced by an empty tuple 
without changing the body of the function.

There are two operations I might perform on the dictionary -

1. iterate over the keys and retrieve the values

2: use 'in' to test if a given string exists as a key

Both of these operations will work on a tuple and give the desired result, 
so it is a very valid workaround.

More testing needed ...

Frank


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 10:01 +0200
  Re: Is this unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-08 20:08 +1000
    Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 12:53 +0200
      Re: Is this unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-09 01:07 +1000
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 07:56 +0200
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 08:51 +0200
          Re: Is this unpythonic? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-09 19:47 +1200
            Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-09 11:10 +0200
    Re: Is this unpythonic? Dave Angel <davea@davea.name> - 2015-05-08 08:04 -0400
      Re: Is this unpythonic? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-10 10:04 +0200
        Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-10 10:58 +0200
          Re: Is this unpythonic? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-10 11:54 +0200
            Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-10 12:40 +0200
    Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 15:12 +0200

csiph-web