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


Groups > comp.lang.python > #90284

Re: Is this unpythonic?

From "Frank Millman" <frank@chagford.com>
Subject Re: Is this unpythonic?
Date 2015-05-10 12:40 +0200
References (2 earlier) <mii4il$vv$1@ger.gmane.org> <mailman.239.1431086688.12865.python-list@python.org> <min3f0$2gh$1@news.albasani.net> <mailman.302.1431248365.12865.python-list@python.org> <min9t3$e56$1@news.albasani.net>
Newsgroups comp.lang.python
Message-ID <mailman.306.1431256992.12865.python-list@python.org> (permalink)

Show all headers | View raw


"Johannes Bauer" <dfnsonfsduifb@gmx.de> wrote in message 
news:min9t3$e56$1@news.albasani.net...
On 10.05.2015 10:58, Frank Millman wrote:

> > 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

> I do understand what you're trying to do, but it is my gut-feeling that
> you're overengineering this and as a side-effect introducing new problems.

This has actually gone beyond a practical suggestion, and become more of an 
academic exercise, so I don't think 'overengineering' comes into it. No-one 
is recommending that this should be used in real-world code.

> With the above declaration as you describe, the code becomes weird:
>
> foo = EMPTY_LIST
> foo.append(123)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'tuple' object has no attribute 'append'
>
> and
>
> foo = EMPTY_DICT
> foo["bar"] = "moo"
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'tuple' object does not support item assignment

The whole point of this admittedly odd declaration is that no-one should do 
anything with it at all. It is simply a place-holder to be used in the 
absence of a real list or dict provided by the caller of the function. The 
starting premise was that the function would only read from the list/dict, 
not try to modify it.

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