[Edu-sig] Properties use case

John Zelle john.zelle at wartburg.edu
Sun Mar 19 03:10:04 CET 2006

On Saturday 18 March 2006 16:39, Michael Tobis wrote:
> So, is there a problem with wrapping them thus:
> ###
> class mcx(object):
>   def  __init__(self,val):
>      self.val = complex(val)
>   def __add__(self,other):
>       """ and similarly for most other special methods """
>      return self.val.__add__(other)
> ###

This reminds me of a question I have with new-style classes. With classic 
Python classes, we can do something like this even more simply with getattr 

class Mutable:

    def __init__(self, value):
        self.value = value

    def __getattr__(self,name):
        return getattr(self.value, name)

This saves me the trouble of having to specifically reimplement all of the 
methods just so that I can trivially pass the work off to self.value. 
Everything works just fine:

>>> from mutable import Mutable
>>> x = Mutable(3+5j)
>>> x
>>> x + (4+7j)
>>> print x
>>> x.value = 3
>>> x + 4

I've used this technique to good advantage when mixing objects from different 
toolkits. My question is, is there an easy way to do this with new-style 

While I'm on the thread, let me just add my 2 cents on Arthur's "creepy" 
mutable complex type. I understand all of the arguments about the potential 
pitfalls of mutable types (I'm one of those "CS types," don't you know :-). 
But OOP is all about mutable objects. As a _design_ decision, if a mutable 
complex will streamline my system, or make it more intuitive, then I wouldn't 
hesitate to do it. 

I think the "creepiness" here is just in the name.  We don't expect complex 
numbers to be mutable. Let's call the new type a complex container with 
auto-unboxing. Does anyone object to a mutable container? I hope not, we used 
to just call them variables. Since the contents are isomorphic to complex, it 
could be handy to have the container object also duck-type to a complex (or 
whatever else it contained). I don't see any creepiness in that. Of course, 
this kind of thinking could get out of hand. The next thing you know people 
might use these to simulate wacko things like reference parameters (oh, the 
horror! :-)
Just my 2 cents.

John M. Zelle, Ph.D.             Wartburg College
Professor of Computer Science    Waverly, IA     
john.zelle at wartburg.edu          (319) 352-8360  

More information about the Edu-sig mailing list