Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90277
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Subject | Re: Is this unpythonic? |
| Date | 2015-05-10 10:58 +0200 |
| References | <mailman.224.1431072121.12865.python-list@python.org> <554c8b0a$0$12992$c3e8da3$5496439d@news.astraweb.com> <mii4il$vv$1@ger.gmane.org> <mailman.239.1431086688.12865.python-list@python.org> <min3f0$2gh$1@news.albasani.net> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.302.1431248365.12865.python-list@python.org> (permalink) |
"Johannes Bauer" <dfnsonfsduifb@gmx.de> wrote in message news:min3f0$2gh$1@news.albasani.net... 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 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] A fair point. How about this as an alternative? If one were to use this technique at all, it would be necessary to add a comment at the top explaining the reason for this odd declaration. It is then a simple extra step to say - EMPTY_L:IST = () and if required - EMPTY_DICT = () and expand the explanation to show why a tuple is used instead. So if there was a situation where the overhead of testing for None became a problem, this solution offers the following - 1. it solves the 'overhead' problem 2. it reads reasonably intuitively in the body of the program 3. it is safe 4. it should not be difficult to write a suitable self-explanatory comment Frank
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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