3 Suggestions to Make Python Easier For Children

Chris Angelico rosuav at gmail.com
Tue Aug 5 09:08:34 EDT 2014


On Tue, Aug 5, 2014 at 10:43 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Because it's horrible and a bad idea.
>
> d = {'this': 23, 'word': 42, 'frog': 2, 'copy': 15, 'lunch': 93}
> e = d.copy()
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'int' object is not callable
>
>
> Conflating keys in a database with object attributes is one of the classic
> blunders, like getting involved in a land war in Asia. It is, *maybe*,
> barely acceptable as a quick-and-dirty convenience at the interactive
> interpreter, in a Bunch or Bag class that has very little in the way of
> methods or behaviour, but not acceptable for a class as fundamental and
> important as dict. No matter what Javascript thinks.

It's not fundamentally bad, just fundamentally incompatible with any
other use of methods. Really, what you're pointing out isn't so much a
problem with conflating keys and attributes as it is with using
attributes for two purposes: key lookup, and method lookup. And that's
*always* going to be a bad thing. Imagine this class:

class BadDict(dict):
    def __getitem__(self, key):
        if key == 'copy': return self.copy
        return super().__getitem__(key)

Now I've just gone the other way - overloading square brackets to
sometimes mean key lookup, and sometimes attribute lookup. And it's
the two meanings on one notation, not the two notations for one
meaning, that's the bad idea.

ChrisA



More information about the Python-list mailing list