Abstract base class
Some object-oriented languages, such as Java, support interfaces, can declare a class that supports a given method or a given access protocol. Abstract base classes (or ABCs) are the same features in Python. The abstract base class consists of the ABC module and contains a metaclass called abcmeta. This metaclass is specially processed by built-in isinstance () and issubclass (), and contains a number of basic abstract base classes that will be widely used by Python developers. In future Python versions, more abstract base classes may be added.
For example, you want to know whether a specific class supports access of the dictionary type. However, the dictionary type is a fuzzy representation. It may mean that it can be accessed through OBJ [1. Does that also mean that values like OBJ [2] = value also work? Or will the object have the keys (), values (), and items () methods? What about iteration variants such as iterkeys (), copy (), and update? What about ITER () through object iteration?
The collections module of Python 2.6 contains many different abstract base classes to express these differences. Iterable indicates that a class defines _ ITER _ (). Container means that the class defines the _ contains _ () method. Therefore, the X in Y expression is supported. Basic dictionary interfaces include data access and keys (), values (), and items (), which are defined by mutablemapping abstract base classes.
You can let your class inherit a specific abstract base class to indicate that they support abstract base class interfaces:
Import collections
Class storage (collections. mutablemapping ):
...
In addition, you can register the class by calling the Register () method of the abstract base class without inheriting the base class.
Import collections
Class storage:
...
Collections. mutablemapping. Register (storage)
The inheritance from the abstract base class may be clearer than the class you write. When you have written a new abstract base class that can describe an existing type or class, or you want to declare that some third-party classes implement an abstract base class,
The Register () method is useful. For example, if you define a printabletype abstract base class, the following are valid:
# Register Python's types
Printabletype. Register (INT)
Printabletype. Register (float)
Printabletype. Register (STR)
Classes should follow the semantics specified by the abstract base class, but Python cannot check this. Class authors should understand the requirements of the abstract base class and implement the Code accordingly.
To check whether an object supports a specific interface, you can write as follows:
Def func (d ):
If not isinstance (D, collections. mutablemapping ):
Raise valueerror ("Mapping object expected, not % R" % d)
Do not think that you must write a lot of Checking code as in the above example. Python has a strong duck-typing tradition and never performs explicit type checks. The code is just a simple method to call objects.
The method will exist. If it does not exist, an exception will be thrown. Be cautious when checking abstract base classes. It is best to do that when necessary.
You can use ABC. abcmeta as metaclass in the class definition to write your own abstract base class:
From ABC import abcmeta, abstractmethod
Class drawable ():
_ Metaclass _ = abcmeta
@ Abstractmethod
Def draw (self, X, Y, scale = 1.0 ):
Pass
Def draw_doubled (self, x, y ):
Self. Draw (X, Y, size = 2.0)
Class square (drawable ):
Def draw (self, X, Y, scale ):
...
In the above drawable abstract base class, the draw_doubled () method is drawn based on twice the object size and can be implemented by calling the drawable method. Therefore, classes that implement this abstract base class are not required.
Provide their own draw_doubled () implementations, although they can do that. However, the implementation of draw () is necessary. abstract base classes cannot provide a useful general implementation.
You can apply the @ abstractmethod modifier in required methods, such as draw (). python will throw an exception to classes that do not define this method. Note that an exception is thrown only when you try to create a subclass instance but do not use this method.
>>> Class circle (drawable ):
... Pass
...
>>> C = circle ()
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
Typeerror: Can't instantiate abstract class circle with abstract methods draw
>>>
Abstract data attributes can be declared with @ abstractproperty decorator:
From ABC import abstractproperty
...
@ Abstractproperty
Def readonly (Self ):
Return self. _ x
The subclass must define a readonly () attribute.
==========================================
Http://bbs.chinaunix.net/thread-771123-1-1.html
Abstract classes in Java are used to implement abstract classes and templates in C ++.
The interface is used to solve the problem that Java cannot inherit more.
It is clear that the landlord started to access object-oriented programming from Java. In fact, Java's "interface" is a special case rather than a common phenomenon. What else should I do if I can inherit more?
In fact, python is the "Object-Oriented" most in line with the actual logic"
Python allows multi-inheritance. In reality, you are both a citizen and a taxpayer. We directly use these "classes" without the need to create special "taxpayer interfaces"
All classes in Python are abstract classes, or there is no abstract class at all. Class methods can be directly used. The class itself has been instantiated during definition. You can enter: A certain type of [Press enter] to see its memory handle. This is true and concise.
In C ++ and Java, after a class is defined, it will definitely occupy the memory space, but it will not be instantiated at the same time. If it is to be used, it will have to be instantiated once, it also occupies some memory space. The usage of memory space occupied by class definition is very low.
Python does not have the concept of "base class", and there is no single root, no basic type. Everything is an object.
Python is the most perfect embodiment of "God", "God", and "only God. What do you believe in? What is love? There is no restriction, but there cannot be any special.
In addition, Python does not mean to imitate Java interfaces, because it is completely unnecessary. The standard classes of Python fully include all functions of interfaces in Java. It would be useful to imitate the C ++ template.
==========================================
Python interface Example
Http://pypi.python.org/pypi/zope.interface
Use Zope. Interface in twisted \ Internet \ interface. py of twisted