Static Typing in Python

Heather Coppersmith me at privacy.net
Mon Mar 15 06:38:40 EST 2004


On Sat, 13 Mar 2004 12:47:43 -0500,
"Sean Ross" <sross at connectmail.carleton.ca> wrote:

> # trivial example based on http://io.kicks-ass.net/LocalVariables
> class A:
>     def __init__(self):
>         self.area = None
>     def square(self, side):
>         self.arae = side * side     # spelling mistake creates new attribute
>     def circle(self, radius):
>         self.area = 3.14 * radius * radius

> a = A()
> a.circle(1)
> print a.area  # 3.14 -> ok
> a.square(4)
> print a.area  # 3.14 -> unintended

What about __slots__ (with recent pythons and new styly classes)?

    >>> class A(object):
    ...  __slots__ = 'area'
    ...  def __init__(self):
    ...   self.arae = None
    ... 
    >>> a = A( )
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 4, in __init__
    AttributeError: 'A' object has no attribute 'arae'

Regards,
Heather

-- 
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli



More information about the Python-list mailing list