instance creatiion as an "event"

Arthur ajsiegel at optonline.net
Tue Apr 22 00:34:30 CEST 2003


Alex writes-

>I'm somewhat confused, I think -- is your problem:

>[A] how do I distinguish whether an operation is being performed "at   the
interactive prompt" or elsewhere?

>and/or is it:

>[B] how do I find out when an instance of a class has been created (quite
apart from the issue of whether it's created at the interactive prompt or
within some module),

>and/or is it:

>[C] how do I find out all the existing instances of a given class at any
given point (during an interactive session), whether that is the time  of
the creation of a new >instance,or not

>Each of these tasks is quite feasible -- some trivially easy (B), some
devilishly hard and tricky (A), some not too bad (C).  But unless you
clarify which one (or more) of >these tasks you want to perform, it's quite
hard to offer you appropriate suggestions

Let me try to specify.

First, it is necessary to understand that the VPython (visual) module is
capable of immediate mode rendering from the intereactive prompt.

>>> from visual import *
Visual-2003-03-15
>>> s=sphere(pos=(1,2))   # a display window is opened and a sphere at
coords (1,2) is rendered
>>> s.pos=(-2,1)                #the sphere moves to coords (-2,1)

My app is using VPython as a rendering backend, with the rendering code
wrapped is classes.

So I have a module, say,  "test_point.py"

"""

from visual import sphere

points=[]
class Point:
   def __init__(self,x,y,z=0):
      points.append(self)
      self.x=x
      self.y=y
      self.z=z
      self.sphere=sphere()

def  find_coords(self):
    pass

def redraw(self):
     self.find_coords()
     self.sphere.pos=(self.x,self.y)

"""

And a module "test_draw.py"


"""

# a gui module, which includes, in essense:

from test_point import *

def draw_all():
   for point in points:
      point.redraw()

if __name__=='__main__':
   p1=TestPoint(1,2)
   p2=TestPoint(-2,-1)
   draw_all()

"""

"python test_draw.py"

and I have 2 points, one at (1,2) and one at (-2,-1).

In the "real world", a point might have 3 planes as its construction
arguments, so that there is some work to be done in coming to the (x,y,z)
coords as the point of intersection of the planes.And there is a factory
function to analyze the given arguments and call the child of an  abstract
Point class that accepts the given arguments and processes them with the
appropriate "find_coords" method.

All is well, until I decide I want to recover some of VPython's immediacy at
the interactive prompt.

>>> from test_draw import *
Visual-2003-03-15
>>> p1=TestPoint(1,2)
>>> p2=TestPoint(-2,1)
# what I have is 2 spheres at default location (0,0,0) - the result of
self.sphere=sphere()
>>> draw_all()
# which now puts the points where they belong.

All I am trying to accomplish is eliminate the need for the explicit call to
draw_all at the command prompt after creating each instance.

I could, of course, add self.redraw() to TestPoint.__init__.  But it seems,
because of the overall "real world" design where TestPoint is somewhere down
an inheritance chain, I need to call this explicitly on the top level
class - and need to do some restructuring of the inheritance scheme, because
if self.redraw is in a base class's __init__ and that base class's
find_coords methods deals with attributes specific to that class - then
super.__init__ ends up with attribute errors, despite the fact that the
find_coords method is overriden in the child.  This was somehow surprising
to me - and where I might be missing something easy and fundamental.

Being stumped there, and lazy, I was looking for some interactive magic.

so that p1=TestPoint(1,2) would somehow register as an event calling
draw_all().

I on one hand want an easy answer.  On the other hand hoping it is not *so*
easy that I will have embarrassed myself by having made a mountain of a
molehill.

Art









More information about the Python-list mailing list