Home >Backend Development >Python Tutorial >Does python have no interface?
The interface only defines some methods but does not implement them. It is mostly used in program design. It only designs what functions are needed, but does not implement any functions. These functions need to be inherited by another class (B). Class B is used to implement one or all of the functions.
Python does not have an interface, but in Python abstract classes and abstract methods implement interface functions. Interfaces cannot be instantiated and can only be used by other Class inheritance to implement corresponding functions. (Recommended learning: Python video tutorial)
Personally, I think interfaces are not that important in python, because if you want to inherit the interface, you need to add each method in it Implement all of them, otherwise a compilation error will be reported. It is better to directly define a class in which all method implementations are pass, and let subclasses rewrite these functions.
Of course, if there is a mandatory requirement that all implementation classes must be written according to the definition in the interface, then the interface must be used.
Use abstract classes and abstract functions to implement methods
#抽象类加抽象方法就等于面向对象编程中的接口 from abc import ABCMeta,abstractmethod class interface(object): __metaclass__ = ABCMeta #指定这是一个抽象类 @abstractmethod #抽象方法 def Lee(self): pass def Marlon(self): pass class RelalizeInterfaceLee(interface):#必须实现interface中的所有函数,否则会编译错误 def __init__(self): print '这是接口interface的实现' def Lee(self): print '实现Lee功能' def Marlon(self): pass class RelalizeInterfaceMarlon(interface): #必须实现interface中的所有函数,否则会编译错误 def __init__(self): print '这是接口interface的实现' def Lee(self): pass def Marlon(self): print "实现Marlon功能"
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of Does python have no interface?. For more information, please follow other related articles on the PHP Chinese website!