Assignement Overloading

Pierre Denis Pierre-et-Liliane.DENIS at village.uunet.be
Sun Jul 8 16:26:59 EDT 2001


I am trying to emulate some kind of type checking in Python (sorry for the heresy). Because of the dynamic typing, my unpretentious
goal is to intercept the assignment statement in order to perform a consistency check between the variable and the object to assign.
Unfortunately, there is no mechanism in Python to do this; there is no magic function like __assign__(name,value). The reason
usually invoked about this lack is the fact that Python's assignment is actually just a name binding, which is very different, for
instance, from C++ assignment. However, there exists a concept very similar in Python, although less general : the __setattr__
method. For a given class instance, it is invoked whenever an attribute assignment is attempted. This allows to make a step towards
a solution to my problem. Consider the following example where an attribute's type checking is emulated, basing on Python's type()
function (this example has no other pretension than showing a 'pattern' to fake assignment overloading) :

--------------------------------------------------------------------------------
''' file typechecker.py
'''
class TypedNamespace:

      def __setattr__(self,name,value):
          if self.__dict__.has_key(name):
             targetType, valueType = type(self.__dict__[name]), type(value)
             if targetType != valueType:
                print "Error : cannot assign %s value to %s variable '%s'." \
                      % (valueType.__name__, targetType.__name__, name)
                return
          self.__dict__[name] = value

t = TypedNamespace()
--------------------------------------------------------------------------------
>>> from typechecker import t
>>> t.name = 'To infinity and beyond'
>>> t.name = 20
Error : cannot assign int value to string variable 'name'.
>>> t.name
'To infinity and beyond'
>>> t.name = ['Hello','Dolly']
Error : cannot assign list value to string variable 'name'.
>>> t.height = 15
>>> t.height = t.name
Error : cannot assign string value to int variable 'height'.
>>>
--------------------------------------------------------------------------------

This works fine, but the (expensive) price to pay is the fact that you can not work with true variables : the prefix 't.' is an
artifact to get true attributes in order to take advantage of the __setattr__ method.

Now I have two questions :

1. Why is there no general way to overload the assignment in Python ? I do not agree with the 'name binding' argument because, to my
humble opinion, __setattr__ allows to overload some assignments, while performing a name binding (in an object's namespace). So I
think the problem is well stated and the request makes sense.

2. Is there other features in Python I can use to emulate assignment overloading (simpler than the technique given in my example) ?

Thanks in advance,

Pierre Denis









More information about the Python-list mailing list