Python's object-oriented advanced------reflection (DAY26)

Source: Internet
Author: User
Tags instance method

a Classmethod
class Classmethod_demo ():     ' Dog '     @classmethod    def  func (CLS):        print(cls.role) Classmethod_demo.func ()
Staticmethod
class Staticmethod_demo ():     ' Dog '     @staticmethod    def  func ():        print("  When normal method is used ") Staticmethod_demo.func ()

The difference between Classmethod and Staticmethod

Classmethod: We call it the class method.

Staticmethod: We call it the static method.

In Python, both static and class methods can be accessed through class objects and class object instances, but the difference is:

[Email protected]: is a function modifier, which means that next is a class method, and for the usual we see is called instance method, the parameter of the class method is CLS, and the argument of the instance method is self, it represents an instance of the class

2. The normal object method requires at least one self parameter that represents the Class object instance

3. Class methods have class variables CLS incoming so that you can do some related processing with the CLS, and there are subclass inheritance is, when calling the class method, the passed-in class variable CLS is a subclass instead of the parent class for the class method, can be called through the class, like C.F (), a bit like a static method in C + +, It can also be called through an instance of the class, like C (). f (), here C (), written so that it is an instance of the class.

4. Static method is not, it is basically the same as a global function, generally used very little

Two. Isinstance and Issubclass

Isinstance (OBJ,CLS) Check if object is a class

class Foo (object):      Pass = foo ()  sinstance (obj, foo)   

Issubclass (sub,super) Check if the class of sub is a derived class of super class

class Foo (object):     Pass class Bar (Foo):     Pass Issubclass (Bar, Foo)

Three. Reflection

In Python everything is object, you can use reflection. Object-oriented Reflection: manipulating object-related properties in the form of strings

Two very important methods : Hasattr and GetAttr (must master) setattr delattr (Learn)

classfoo:f='Static variables of the class'    def __init__(self,name,age): Self.name=name Self.age= AgedefSay_hi (self):Print('hi,%s'%self.name) obj=foo ('Egon', 73)#detect if a property is includedPrint(Hasattr (obj,'name'))Print(Hasattr (obj,'Say_hi'))#Get PropertiesN=getattr (obj,'name')Print(n) Func=getattr (obj,'Say_hi') func ()Print(GetAttr (obj,'aaaaaaaa','It doesn't exist.'))#Error#Setting PropertiesSetAttr (obj,'SB', True) setattr (obj,'Show_name',Lambdaself:self.name+'SB')Print(obj.__dict__)Print(Obj.show_name (obj))#Delete PropertyDelattr (obj,' Age') delattr (obj,'Show_name') delattr (obj,'show_name111')#does not exist, then an errorPrint(obj.__dict__) Four ways to use a demo

Python's object-oriented advanced------reflection (DAY26)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.