Class Chaos

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Jun 28 20:58:18 CEST 2004


"Leif K-Brooks" <eurleif at ecritters.biz> wrote in message
news:2kb09jF3t3eU2 at uni-berlin.de...
> Paul McGuire wrote:
> > You will also find that you avoid some funny Python behavior if you
avoid
> > using built-in type names (such as list, dict, and str) for variable
names.
>
> What sort of funny behavior?

Unexpected masking of the built-in classes, their static methods and
constructors.  F'rinstance:

>>> str.join("",["A","B","C"])
'ABC'
>>> str = "X"
>>> str.join("",["A","B","C"])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: join() takes exactly one argument (2 given)


Here's a constructor example:

>>> list()
[]
>>> list = [ 1,2,3,4]
>>> list()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'list' object is not callable


Here's a more insidious constructor collision example (admittedly contrived,
but note, this one doesn't raise an exception!):

>>> list()
[]
>>> list = (lambda a=10: [ i for i in range(a)] )
>>> list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


It is too bad that these class names are *so* natural and intuitive, that
one just *wants* to name some temporary list as 'list' or dict as 'dict'.

-- Paul





More information about the Python-list mailing list