Python-Ref > Object oriented programming > Basics > Special methods
 
 

<-^^->
Moduly
Knihovní funkce

Special methods

Magic methods that can influence the behaviour of objects in common operations
Python allows you to implement several special methods in your classes that can influence how the object responds to common operations, such as str(), comparison, summation, multiplication, etc.
Expand/Shrink
Zdroj: (oop5-1.py)
  1   class Rectangle:
  2   
  3     def __init__( self, width, height):
  4       self.width = width
  5       self.height = height
  6   
  7     def area( self):
  8       return self.width * self.height
  9   
 10   r1 = Rectangle( 1, 1)
 11   r2 = Rectangle( 1, 1)
 12   r3 = Rectangle( 3, 4)
 13   r4 = Rectangle( 4, 0)
 14   
 15   print "%-22s" % "r1", r1
 16   print "%-22s" % "r3", r3
 17   print "%-22s" % "r1 == r2", r1 == r2
 18   print "%-22s" % "r2 != r3", r2 != r3
 19   print "%-22s" % "r3 < r4", r3 < r4
 20   print "%-22s" % "bool( r3), bool( r4)", bool( r3), bool( r4)
stdout:
r1                     <__main__.Rectangle instance at 0x2b7f6cb99f38>
r3                     <__main__.Rectangle instance at 0x2b7f6cbaa998>
r1 == r2               False
r2 != r3               True
r3 < r4                True
bool( r3), bool( r4)   True True
Doba běhu: 20.4 ms
Expand/Shrink
Zdroj: (oop5-2.py)
  1   class Rectangle:
  2   
  3     def __init__( self, width, height):
  4       self.width = width
  5       self.height = height
  6   
  7     def area( self):
  8       return self.width * self.height
  9   
 10     def __str__( self):
 11       """this implements what will str() function make out of this object"""
 12       return "Square %d * %d" % (self.width, self.height)
 13   
 14     def __eq__( self, other):
 15       """implements behaviour of equality comparison"""
 16       if self.width == other.width and self.height == other.height:
 17         return True
 18       else:
 19         return False
 20   
 21     def __ne__( self, other):
 22       """implements non-equality comparison"""
 23       return not self.__eq__( other)
 24   
 25     def __nonzero__( self):
 26       """when is the object regarded as nonzero?"""
 27       if self.area() != 0:
 28         return True
 29       else:
 30         return False
 31   
 32     def __gt__( self, other):
 33       """when a rectangle is greater than other"""
 34       if self.area() > other.area():
 35         return True
 36       return False
 37   
 38     def __lt__( self, other):
 39       """when a rectangle is lower than other"""
 40       if self.area() < other.area():
 41         return True
 42       return False
 43       
 44   r1 = Rectangle( 1, 1)
 45   r2 = Rectangle( 1, 1)
 46   r3 = Rectangle( 3, 4)
 47   r4 = Rectangle( 4, 0)
 48   
 49   print "%-22s" % "r1", r1
 50   print "%-22s" % "r3", r3
 51   print "%-22s" % "r1 == r2", r1 == r2
 52   print "%-22s" % "r2 != r3", r2 != r3
 53   print "%-22s" % "r3 < r4", r3 < r4
 54   print "%-22s" % "bool( r3), bool( r4)", bool( r3), bool( r4)
stdout:
r1                     Square 1 * 1
r3                     Square 3 * 4
r1 == r2               True
r2 != r3               True
r3 < r4                False
bool( r3), bool( r4)   True False
Doba běhu: 21.7 ms