Ducks type
If it looks like, sounds like and walks like a duck, then it is a duck '. python programmers usually write programs based on this behavior. For example, if you want to write a custom version of an existing object, the object can be inherited, you can create a look and act like, but it is not the object of any new relationship, which is usually used to save the program of loosely coupled components.
Example 1: using 'like a file of an object of various criteria defined in the library, despite how these objects like files, but they do not have built-in file object inherit Method Example 2: There are several types of sequence forms: string, lists, tuples, but they are not directly inherit a direct relationship # both like a duck, looks like both files, so as you can when the files go with
class TxtFile: def read(self): pass def write(self): pass class DiskFile: def read(self): pass def write(self): pass
In Python does not advocate to restrain through inheritance
For example, a list, a string, a tuple has index method
[].index() ''.index() ().index()
View index Source
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0
Ducks Type Summary:
-
Not by inheritance to the specific constraints which must have some class method name, is through the concept of a convention to ensure that a similar feature called multiple classes in the same name
-
len (): function to receive data types are str, list, tuple, set, dict, range (3), these are the types of duck
class A: DEF func1 (Self): Pass DEF func2 (Self): Pass DEF func3 (Self): Pass A = A () B = A () Print (A.func1) Print (a.func1) Print (B. func1) '' ' performs an output: <A.func1 AT 0x00000184450715E8 function> . <Method A.func1 of bound <__ __ A main Object AT 0x00000184451D4508 >> . <Method A.func1 of bound <__ __ main Object AT 0x00000184451D4588 >> A ' ' ' # bound binding represents the mean. When A class is instantiated, the method which will make do bind class.
Class method, since the object attribute memory space is not applicable, it is not tied to the objects and methods, but rather tied classes and methods
class A: DEF func1 (Self): Pass # binding between an object and a method @classmethod DEF func2 (Self): Pass # binding between classes and methods @classmethod DEF func3 (Self): Pass A = A ( ) B = A () Print (a.func2) Print (a.func2) # Object classes inside tied to find methods. Print (b.func2) '' ' performs an output: <method a.func2 bound of <class 'in __main __. A' >> <A.func2 bound Method of <class 'in __main __. A' >> <A.func2 bound Method of <class 'in __main __. A' >> '''
Static methods are not binding method does not take place any object or class and binding relationship
class A: DEF func1 (Self): Pass # binding between an object and a method @classmethod DEF func2 (Self): Pass # binding between classes and methods @staticmethod DEF func3 (Self): Pass # static method a = A () B = A () Print (A.func3) Print (a.func3) Print (b.func3) '' ' performs an output: <0x000001D7C46EFAF8 function A.func3 AT> <AT 0x000001D7C46EFAF8 function A.func3> < AT 0x000001D7C46EFAF8 A.func3 function> '' '
python objects everywhere
'A' is the object str
[2,3] is the list of objects