Although Python has the advantage of having other languages in function programming, let's not forget that Python is an OO language, too. So while we're looking at the advantages of Python in FP, we have to look at Python's features in Oo.
To discuss the oo nature of Python, it's natural to understand the class in Python. Defining a class in Python and creating an object instance are simple, with the following code:
Class Grandpa: def __init__ (self): print (' I\ ' M grandpa ') class Father (Grandpa): def-__init__ (self): print (' i\ ' m father! ') Class Son (Father): "" " A Simple Example Class" "" i = 12345 def __init__ (self): print (' This is the constructor, Son ') def SayHello (self): return ' Hello World ' if __name__ = = ' __main__ ': son = Son () # type help info print (' type help info: ', son.__doc__) #类型名称 print (' type name: ', son.__name__) #类型所继承的基类 print (' The base class to which the type inherits: ', son.__bases__) #类型字典 print (' Type dictionary: ', son.__dict__) #类型所在模块 print (' type module: ', son.__ module__) #实例类型 print (' instance type: ', Son (). __class__)
Operating conditions:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32
Type "Copyright", "credits" or "license ()" For more information.
>>> ================================ RESTART ================================
>>>
This is a constructor, son.
Type help information: A Simple Example Class
Type name: Son
The base class that the type inherits from: ( ,)
Type dictionary: {' __module__ ': ' __main__ ', ' SayHello ': , ' __doc__ ': ' A Simple Example class ', ' __init__ ': , ' I ': 12345}
Type module: __main__
This is a constructor, son.
Instance type:
>>>
#Python支持多重继承
First of all 1th, you will find that the definition of class has a parenthesis, which is the embodiment of the inheritance of the place. Java uses extends,c#, C + + with colons (:), Python uses parentheses. There are two values from the parentheses, and you can be sure of that: Python supports multiple inheritance;
#__init__是Class中的构造函数
2nd, __init__ is a constructor in class, and two different forms of constructors embody Python's support for function overloading. In the constructor, there is a special parameter, self, whose meaning is the same as the one we common in Java and C #. One thing to emphasize here is that the method defined in class is essentially a function, but it must include the self parameter when the method is defined, and the self parameter must be placed first;
#python成员变量
3rd, in Python, you don't need to explicitly declare the data members of class, but when you assign a value, the assigned variable becomes the data memebers of the class, just like X and y in the code. Not only do you need to explicitly declare the data members, more specifically, you can even delete the data memebers in class by using the Del method. When I first saw such a feature, I was surprised. After all, Oo is the first one is encapsulated, but this feature is not to destroy the characteristics of the package?
#python方法二义性问题
4th, because Python supports multiple inheritance, there is a possibility of a method ambiguity [1]. However, due to the fact that Python follows the principle of depth-first search, the ambiguity of the method is well avoided. For example, in the above code, MyClass also inherits from Baseclassa and BASECLASSB, assuming that MyClass calls a method called Derivedmethod, Derivedmethod are defined in both Baseclassa and BASECLASSB, and signature are identical, the methods in Baseclassa are called. If Derivedmethod is not defined in Baseclassa, but Baseclassa's parent class defines this method, it will be derivedmethod called in the parent class of Baseclassa. In summary, the path to the inheritance method search is first left to right, and after a BaseClass is selected, it will be searched along the BaseClass inheritance structure, up to the top, and then to another baseclass.
So much for the first time, the features of OO in Python will be described further in the post.
[1] Method ambiguity: Because a class inherits from two or more parent classes, and there are signature identical methods in these parent classes, the compiler will not be able to determine which parent class the subclass inherits from, leading to the method ambiguity problem