0% found this document useful (0 votes)
5 views1 page

Dsaa 25

Uploaded by

farawayfromhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Dsaa 25

Uploaded by

farawayfromhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

5 Operator Overloading 9

44 def main():
45 boyDog = Dog("Mesa", 5, 15, 2004, "WOOOOF")
46 girlDog = Dog("Sequoia", 5, 6, 2004, "barkbark")
47 print(boyDog.speak())
48 print(girlDog.speak())
49 print(boyDog.birthDate())
50 print(girlDog.birthDate())
51 boyDog.changeBark("woofywoofy")
52 print(boyDog.speak())
53 puppy = boyDog + girlDog
54 print(puppy.speak())
55 print(puppy.getName())
56 print(puppy.birthDate())
57
58 if __name__ == "__main__":
59 main()
Listing 1.7 Operator Overloading for the Dog Class

This text uses operator overloading fairly extensively. There are many operators
that are defined in Python. Python programmers often call these operators Magic
Methods because a method automatically gets called when an operator is used in an
expression. Many of the common operators are given in the table in Fig. 1.4 for your
convenience. For each operator the magic method is given, how to call the operator
is given, and a short description of it as well. In the table, self and x refer to the same
object. The type of x determines which operator method is called in each case in the
table.

Method Definition Operator Description


__add__(self,y) x+y The addition of two objects. The type of x determines which add
operator is called.
__contains__(self,y) y in x When x is a collection you can test to see if y is in it.
__eq__(self,y) x == y Returns True or False depending on the values of x and y.
__ge__(self,y) x >= y Returns True or False depending on the values of x and y.
__getitem__(self,y) x[y] Returns the item at the yth position in x.
__gt__(self,y) x>y Returns True or False depending on the values of x and y.
__hash__(self) hash(x) Returns an integral value for x.
__int__(self) int(x) Returns an integer representation of x.
__iter__(self) for v in x Returns an iterator object for the sequence x.
__le__(self,y) x <= y Returns True or False depending on the values of x and y.
__len__(self) len(x) Returns the size of x where x has some length attribute.
__lt__(self,y) x<y Returns True or False depending on the values of x and y.
__mod__(self,y) x%y Returns the value of x modulo y. This is the remainder of x/y.
__mul__(self,y) x*y Returns the product of x and y.
__ne__(self,y) x != y Returns True or False depending on the values of x and y.
__neg__(self) -x Returns the unary negation of x.
__repr__(self) repr(x) Returns a string version of x suitable to be evaluated by the eval
function.
__setitem__(self,i,y) x[i] = y Sets the item at the ith position in x to y.
__str__(self) str(x) Return a string representation of x suitable for user-level interac-
tion.
__sub__(self,y) x-y The difference of two objects.

Fig. 1.4 Python Operator Magic Methods

You might also like