[Edu-sig] quantum instance?

Scott David Daniels Scott.Daniels at Acm.Org
Sun Sep 11 00:30:17 CEST 2005

Arthur wrote:
> Trying to handle the sudden change of state of an instance of an object 
> - a "quantum instance"
> c starts as a Circle instance.
> Say, in the course of the manipulation of c its radius approaches 
> towards infinity, and upon the radius becoming > than some Max, I want c 
> to suddenly think of itself as a Line instance rather than as a Circle 
> instance.
> doable?
> hints?
> Art

As a sketch:

class Blended(object):
     MaxRadius = 123456

     def __init__(self, radius):
         self._active = Circle(radius=radius)
         self._other = Line()
         self.radius = radius  # Switch to Line if necessary

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

     def __setattr__(self, name, value):
	if name[0] == '_':
	    object.__setattr__(self, name, value)
	elif name[0] != 'radius':
	    return setattr(self, value)
         if value < self.MaxRadius:
             if isinstance(self._active, Line):
                 self._active, self._other = self._other, self._active
             if isinstance(self._active, Circle):
                 self._active, self._other = self._other, self._active
         self._active.radius = value

You could read up on __getattr__, __getattribute__, and friends in
the Language References section 3.3.2:
     Customizing attribute access

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

More information about the Edu-sig mailing list