One, add:
@classmethod
The name in the object namespace is not used in the entire method, and the name in the namespace of the class is used (except for normal methods and property properties)
Default parameters for class methods: The CLS calls the class of the postback method
How class methods are invoked: called by the class name
The essence of calling through a class name is a method
@statimethod
Put a normal function in the class and add a @staticmethod adorner to the function.
This function does not need to pass the default parameters: Self,cls
How static methods are invoked: called by the class name
Calling essence through a class name is a function
From types import Methodtype,functiontype with isinstance, judging whether it is a method or a function
Called by the class name, @classmethod is the method, @statimethod is the function
fromTypesImportFunctiontype,methodtypeclassFoo: @classmethoddefFunc1 (CLS):Pass@staticmethoddefFunc2 ():PassPrint('foo.func1-function:', Isinstance (Foo.func1,functiontype))#determine if a class method is a functionPrint('Foo.func1-method:', Isinstance (Foo.func1,methodtype))#determine whether a class method is a methodPrint('----------------------------------------')Print('foo.func2-function:', Isinstance (Foo.func2,functiontype))#determine if a static method is a functionPrint('Foo.func2-method:', Isinstance (Foo.func2,methodtype))#determine whether a static method is a method" "foo.func1-function:falsefoo.func1-method:true----------------------------------------foo.func2-function: Truefoo.func2-method:false" "
Called by the object, @classmethod is the method, @statimethod is the function
fromTypesImportFunctiontype,methodtypeclassFoo: @classmethoddefFunc1 (CLS):Pass@staticmethoddefFunc2 ():PassF=Foo ()Print('foo.func1-function:', Isinstance (F.func1,functiontype))#determine if the object class method is a functionPrint('Foo.func1-method:', Isinstance (F.func1,methodtype))#determine whether an object class method is a methodPrint('----------------------------------------')Print('foo.func2-function:', Isinstance (F.func2,functiontype))#determine whether the object static method is a functionPrint('Foo.func2-method:', Isinstance (F.func2,methodtype))#determine whether an object static method is a method" "foo.func1-function:falsefoo.func1-method:true----------------------------------------foo.func2-function: Truefoo.func2-method:false" "
Class can be called, object cannot be called
deffunc (args):ifcallable (args):Print(args ())Else: Print('Not callable:', args)classFoo:PassF=foo () func (foo)#class <__main__. Foo object at 0x0000016ae645c278>Func (f)#Object Not callable: <__main__. Foo object at 0x0000016ae645c320>
Summarize:
It is the function that needs to explicitly pass the argument, the method (function) that does not need to explicitly pass the argument. A class call is a function, and an instance of a class calls a method (function)
Class method: Class invocation and object invocation are both method (function)
Static methods: Class calls and object calls are function (functions)
#Judging functions, methods, objects fromTypesImportMethodtype,functiontypedefFunc (*args): Function_count=0 Method_count=0 Foo_obj=0 forIteminchargs:ifIsinstance (Item,functiontype): Function_count + = 1elifIsinstance (Item,methodtype): Method_count + = 1elifIsinstance (Item,foo): Foo_obj + = 1return{'Function_count': Function_count,'Method_count': Method_count,'Foo_obj': Foo_obj}defFunc1 ():PassclassFoo:defMethod1 (self):PassF1=Foo () ret=func (Func1,f1.method1,foo.method1,foo (), FUNC,F1)Print(ret)" "{' Function_count ': 3, ' Method_count ': 1, ' Foo_obj ': 2}" "
Parsing of class and object memory mechanisms
Object-oriented reflection of Python