class instance methods, private methods, and abstract methods
The most common use in Python is the class instance method, similar to the class instance property in the attribute, and the same method as the private property, that is, the private method, the two common methods are described below, and a special meaning class instance method-abstract method.
class instance method /Instance Method
The class instance method is defined by the keyword Def, the first parameter is self, and the method can be invoked using the Self/class instance, which is the most basic method
1 class Foo(object):
2 def foo(self):
3 print("This is foo method.")
4
5 f = Foo()
6 f.foo()
The above code defines a Foo class, then defines a method foo for a class instance, in line 5th, generates a class instance F from the class, and then uses F.foo () to implement a call to the class instance method (in addition to this way, through GetAttr/__getattribut E__, etc. to get the Foo method).
Finally, you can see that the contents of the Foo method are printed in the console.
This is foo method.
Private Methods /Private Method
Python does not have the same language as public, private and other keywords to decorate. Defining a private method in Python is similar to a private property, just precede the method with the ' __ ' two underscore. Internally, Python uses a name mangling technique to replace __membername with _classname__membername, so when you use the name of the original private member externally, You will be prompted not to find it.
Note: __membername Here cannot be written as __membername__, otherwise it will not be a private method and become a magic method .
1 class Foo():
2 def __privatemethod(self):
3 print(‘This is a private method.‘)
4
5 def _privatemethod_callable(self):
6 print(‘This is a private method but callable.‘)
7
8 f = Foo()
9 f._Foo__privatemethod()
10 f._privatemethod_callable()
Similar to the previous one, two private methods are defined in the class, which is the same as the private property, where a single underscore means that the declaration is an internal private method, but can still be called externally, while a double underscore indicates that this is a private method of protection. After the definition is completed, an instance of the class is generated, and a private method is called, as can be seen from the way it is called, the role played by name Mangling.
This is a private method.
This is a private method but callable.
Abstract Methods /Abstract Method
The abstract method in Python is a method that defines the base class, and any method that inherits that class needs to redefine the method. The simplest way to define an abstract method is as follows:
1 class Foo():
2 def abmethod(self):
3 raise NotImplementedError
However, the disadvantage of this approach is that it is only possible to detect if the subclass redefined the method when it is called, and this Python provides an ABC module that allows the abstract method to detect the redefinition in the initialization phase (refer to the ABC module section). The method is decorated by @abstractmethod.
Python program Structure (2)--method/method, class instance method, private method, and abstract method