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

Christian Heimes lists at cheimes.de
Mon Oct 18 11:17:52 EDT 2010


Am 18.10.2010 16:35, schrieb fab at slick.airforce-one.org:
> 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


Don't nest classes. Just don't. This might be a valid and good approach
in some programming languages but it's not Pythonic. Your code can
easily be implemented without nested classes.

Christian




More information about the Python-list mailing list