Object default value

Scott David Daniels Scott.Daniels at Acm.Org
Tue Sep 20 17:27:35 EDT 2005


James Stroud wrote:
> I think you want to overload the assignment operator here. I'm not sure that 
> is allowed in python (I've never seen it done)....
> But I don't think assignment overloading is allowed in python:

Exactly correct.  Assignment is an operation on a namespace using a new
value, and does not involve the former value.  Something similar to
assignment overloading is possible with properties, but you need to be
using object attributes, not names in a module (or function locals).

class SomeDemo(object):
     def _read_foo(self):
         print 'Reading foo'
         return self._foo

     def _write_foo(self, value):
         print 'Writing foo as %r' % value
         return self._foo

     def _del_foo(self):
         print 'Deleting foo'
         del self._foo

     foo = property(_read_foo, _write_foo, _del_foo)

obj = SomeDemo()
obj.foo = 123
obj.foo += 1
del obj.foo

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list