Classes in a class: how to access variables from one in another

fab at slick.airforce-one.org fab at slick.airforce-one.org
Mon Oct 18 10:35:58 EDT 2010


Gary Herron <gherron at islandtraining.com> wrote:
> Well, your code still doesn't make sense, but the generic answers are:

I'll clarify what I need then:

I'm drawing Bézier curves. I draw them on a zone that is defined as a
subclass of GtkDrawingArea.

In a zone, I define a system of coordinates by 4 values: xmin, xmax,
ymin, and ymax that define the viewing port.

A curve is defined in this system of coordinates by a collection of
points.

So my way of coding it is the following:

class zone(GtkDrawingArea):

  class systemOfCoordinates:
    self.xmin = -5
    self.xmax = 5
    self.ymin = -5
    self.ymax = 5

  class Curve:
    self.listOfPoints = ()

    def draw(self):
      pass
      # for each point in self.listOfPoints: draw this point
      # then draw the Bézier curve passing through these points 

  class Point:
   def __init__(self, x, y):
     (self.x, self.y) = (x, y)

   def draw(self):
     # ... code for drawing a dot in the system of coordinates...

  def __init__(self): # for the zone object
     self.coord = self.systemOfCoordinates()
     self.listOfCurves = ( self.Curve() )

  def expose(self, widget, event):
     pass
     # for each curve of self.listOfCurves: draw it

Now to actually draw the dot on the screen, I need to access
coord.xmin, coord.xmax, coord.ymin and coord.ymax to Point.draw(). I
could do this by passing a reference to Point.draw(), but I'd like to
do this implicitely.

I guess that in that case, xmin, xmax, ymin and ymax should be class variables of zone, right?

> If *any* object, class or instance of a class (or module or whatever) 
> contains another, access is by chaining the dots.
>   OuterOb.InnerOb.attribute
> 
> Hope that answers your question.

This should help, I'll make some tests.

Thanks.

-- 
F. Delente



More information about the Python-list mailing list