36
loading...
This website collects cookies to deliver better user experience
abc
But that is not for this course.Disclaimer! Advanced concepts like Multiple inheritance, duck typing, abc module will be covered in the advanced part of this course. This part is only for a brief upon those concepts.
Still, a few use cases of interfaces are not resolved fully. If you want to make base classes that cannot be instantiated, but provide a specific interface or part of an implementation, interfaces are a must.
"If it walks like duck, swims like duck, looks like a duck, then it probably should be a duck."
class ape():
food=""
def __init__(self,weight):
self.weight=weight
def cry(self):
pass # Do nothing
def eat(self):
print(self.food)
class chimpanzee(ape):
def __init__(self,weight):
super().__init__(weight)
self.food="bananas"
def cry(self):
print("I am a chimp")
class gorrilla(ape):
def __init__(self,weight):
super().__init__(weight)
self.food="fruits"
def cry(self):
print("I am a gorrilla")
class gorranzee(chimpanzee,gorrilla):
pass
A=gorranzee(20)
print(A.food)
A.cry()
bananas
I am a chimp
class ape():
food=""
def __init__(self,weight):
self.weight=weight
def cry(self):
pass # Do nothing
def eat(self):
print(self.food)
class chimpanzee(ape):
def __init__(self,weight):
super().__init__(weight)
self.food="bananas"
def cry(self):
print("I am a chimp")
class gorrilla(ape):
def __init__(self,weight):
super().__init__(weight)
self.food="fruits"
def cry(self):
print("I am a gorrilla")
class gorranzee(gorrilla,chimpanzee):
pass
A=gorranzee(20)
print(A.food)
A.cry()
fruits
I am a gorrilla
cry()
method and constructor of gorilla class will run. And if chimpanzee is passed first, then the cry and constructor of chimpanzee class will be run.The package zope.interface provides an implementation of “object interfaces” for Python. It is maintained by the Zope Toolkit project. The package exports two objects, ‘Interface’ and ‘Attribute’ directly. It also exports several helper methods. It aims to provide stricter semantics and better error messages than Python’s built-in abc module.
Python follows the EAFP (Easier to Ask Forgiveness than Permission) rather than the LBLY (Look Before You Leap) philosophy. The EAFP is somewhat linked to the "duck typing" style.