Interfaces in python

Source: Internet
Author: User

Interfaces in python

What is an interface?

The interface only defines some methods, but does not implement them. It is mostly used for program design, but only for what functions are required by the design, but does not implement any functions, after these functions are inherited by another class (B), Class B can implement one or all of these functions.

My personal understanding is mostly used for collaborative development. Different people implement various methods in interfaces in different classes.

In python, interfaces are implemented by abstract classes and abstract methods. Interfaces cannot be instantiated and can only be inherited by other classes to implement corresponding functions.

I personally think the interface is not that important in python, because if you want to inherit the interface, you need to implement all the methods in it. Otherwise, a compilation error will be reported. It is better to define a class directly, all the methods are implemented as pass, so that the subclass can rewrite these functions.

Of course, if there are mandatory requirements, all the implementation classes must be written according to the definition in the interface, the interface must be used.

Method 1: use abstract classes and Abstract Functions

 

# Adding an abstract class and an abstract method is equivalent to the interface from abc import ABCMeta and abstractmethodclass interface (object) in Object-Oriented Programming ): _ metaclass _ = ABCMeta # specifies that this is an abstract class @ abstractmethod # abstract method def Lee (self): pass def Marlon (self): passclass RelalizeInterfaceLee (interface ): # All functions in the interface must be implemented; otherwise, the compilation error def _ init _ (self): print 'is the implementation of the interface 'def Lee (self ): print 'implement Lee function' def Marlon (self): pass class RelalizeInterfaceMarlon (interface): # All functions in the interface must be implemented; otherwise, the compilation error def _ init _ (self) will occur): print 'This is the implementation of the interface 'def Lee (self): pass def Marlon (self): print "to implement the Marlon function"

Method 2: define interfaces with common classes,

 

 

Class interface (object): # if this is an interface, the interface name can be defined at will. All subclasses do not need to be implemented in the def Lee (self) function of this class ):, pass def Marlon (self): pass class Realaize_interface (interface): def _ init _ (self): pass def Lee (self ): print "Lee function in implementation interface" class Realaize_interface2 (interface): def _ init _ (self): pass def Marlon (self ): print "the Marlon function in the implementation interface" obj = Realaize_interface () obj. lee () obj = Realaize_interface2 () obj. marlon ()


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.