by 介誠
Polymorphism (多型) means that the meaning of an operation (like a + b
) depends on the objects being operated on.
(Learning Python, Fifth Edition, by Mark Lutz)
print(3 + 4)
7
print("3" + "4")
34
class Car():
def shout(self):
print("I'm a Car!")
class Bus(Car):
def shout(self):
print("I'm a Bus!")
class Truck(Car):
def shout(self):
print("I'm a Truck!")
give_me_a_car = Car()
give_me_a_bus = Bus()
give_me_a_truck = Truck()
give_me_a_car.shout()
I'm a Car!
give_me_a_bus.shout()
I'm a Bus!
give_me_a_truck.shout()
I'm a Truck!
class Car():
def shout(self):
print("I'm a Car!")
class Bus(Car):
def shout(self):
print("I'm a Bus!")
def need_a_push(self):
print("Can someone help?")
class Car():
def shout(self):
print("I'm a Car!")
class Bus(Car):
def shout(self):
print("I'm a Bus!")
def need_a_push(self):
print("Can someone help?")
give_me_a_car = Car()
give_me_a_bus = Bus()
give_me_a_bus.need_a_push()
Can someone help?
give_me_a_car.need_a_push()
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-10-25de065dd6f4> in <module>() ----> 1 give_me_a_car.need_a_push() AttributeError: 'Car' object has no attribute 'need_a_push'
super()
class Car():
def shout(self):
print("I'm a Car!")
class Bus(Car):
def child_shout(self):
print("I'm a Bus!")
def parent_shout(self):
super().shout()
give_me_a_bus = Bus()
give_me_a_bus.child_shout()
I'm a Bus!
give_me_a_bus.parent_shout()
I'm a Car!
super()
class Car():
def __init__(self, wheels_number=4):
self.wheels_number = wheels_number
class Bus(Car):
# override __init__ function
def __init__(self, wheels_number, brand_name):
#super().__init__(wheels_number)
self.brand_name = brand_name
car = Car()
bus = Bus()
bus.wheels_number
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-22f08a0068d6> in <module>() ----> 1 bus.wheels_number AttributeError: 'Bus' object has no attribute 'wheels_number'
class Car():
def __init__(self, wheels_number=4):
self.wheels_number = wheels_number
class Bus(Car):
# override __init__ function
def __init__(self, wheels_number, brand_name):
super().__init__(wheels_number)
self.brand_name = brand_name
give_me_a_bus = Bus(8, 'Benz')
give_me_a_bus.wheels_number
8
give_me_a_bus.brand_name
'Benz'
The names of these special methods begin and end with double underscores ( __ )
e.g. __init__
equals()
¶class Word():
def __init__(self, text):
self.text = text
def equals(self, word2):
return self.text.lower() == word2.text.lower()
ha = Word('ha')
HA = Word('HA')
eh = Word('eh')
ha.equals(HA)
True
ha.equals(eh)
False
__eq__()
¶class Word():
def __init__(self, text):
self.text = text
def __eq__(self, word2):
return self.text.lower() == word2.text.lower()
ha = Word('ha')
HA = Word('HA')
eh = Word('eh')
ha == HA
True
ha == eh
False
__str__()
and __repr__()
¶class Word():
def __init__(self, text):
self.text = text
def __eq__(self, word2):
return self.text.lower() == word2.text.lower()
def __str__(self):
return self.text
def __repr__(self):
return "Word(" + self.text + ")"
class Word():
def __init__(self, text):
self.text = text
def __eq__(self, word2):
return self.text.lower() == word2.text.lower()
def __str__(self):
return self.text
def __repr__(self):
return "Word(" + self.text + ")"
ha = Word('ha')
ha # uses __repr__()
Word(ha)
print(ha) # uses __str__()
ha