Simple class with slots : Class Definition « Class « Python






Simple class with slots

 

class PointWithoutSlots:
   def __init__( self, xValue = 0.0, yValue = 0.0 ):
      self.x = float( xValue )
      self.y = float( yValue )

class PointWithSlots( object ):
   __slots__ = [ "x", "y" ]

   def __init__( self, xValue = 0.0, yValue = 0.0 ):
      self.x = float( xValue )
      self.y = float( yValue )

noSlots = PointWithoutSlots()
slots = PointWithSlots()

for point in [ noSlots, slots ]:
   print "\nProcessing an object of class", point.__class__
   
   print "point.x is:", point.x
   newValue = float( raw_input( "Enter new x coordinate: " ) )
   print "set new x-coordinate value..."

   point.X = newValue

   print "The new value of point.x is:", point.x

   
  








Related examples in the same category

1.Class Time with properties
2.Class Time with customized attribute access.
3.Definition and test function for class Point.
4.User-Defined Classes
5.NameTag class
6.Class instance value setting
7.Define a class