[Base] reflection method python

  1、getattr()

    Function: Get instance attribute value or a method of address
    Return Value: the value of the address attribute / method

    getattr (object attribute name / method name)
    getattr (class, attribute name / method name)

1  class the Person:
 2      Country = " Chinese " 
3  
4      DEF  __init__ (Self, name, Age):
 5          self.name = name
 6          self.age = Age
 7  
8      DEF show_info (Self):
 9          Print ( " My Name: % s, Age: D% " % (the self.name, self.age))
 10  
. 11      @classmethod
 12 is      DEF show_country (CLS):
 13 is          Print (cls.country)
 14  
15  
16 P1 = the Person ("xixi", 7)
17 
18 name = getattr(p1, "name")
19 print(name)  # xixi
20 
21 show_info = getattr(p1, "show_info")
22 print(show_info)  # <bound method Person.show_info of <__main__.Person object at 0x000000000294AD68>>
23 
24 show_info()  # 方法调用   我的姓名: xixi, 年龄: 7
25 
26 print(getattr(Person, "country"))  # 中国
27 
28 show_country = getattr(Person, "show_country")
29 print(show_country)  # <bound method Person.show_country of <class '__main__.Person'>>
30 show_country()  # 中国

  2, Hsattr ()

    Function: detecting whether a class methods, class attributes, attribute instance, an example method
    Return value: BOOL value

    the hasattr (object instance attributes / instance method)
    the hasattr (class, class attributes / class methods)

1  class the Person:
 2      Country = " Chinese " 
3  
4      DEF  __init__ (Self, name, Age):
 5          self.name = name
 6          self.age = Age
 7  
8      DEF show_info (Self):
 9          Print ( " My Name: % s, Age: D% " % (the self.name, self.age))
 10  
. 11      @classmethod
 12 is      DEF show_country (CLS):
 13 is          Print (cls.country)
 14  
15  
16 P1 = the Person ("xixi", 200)
17 
18 print(hasattr(p1, "name"))  # True
19 print(hasattr(p1, "show_info"))  # True
20 print(hasattr(p1, "get_num"))  # False
21 
22 print(hasattr(Person, "country"))  # True
23 print(hasattr(Person, "show_country"))  # True

  3、setattr()

    Function: property value instance /
    Return Value: None

    setattr (object attribute name, attribute value)
    setattr (class, attribute name, attribute value)

    If the property already exists, the re-assignment;
    if the property does not exist, add a property and assignment;

1  class the Person:
 2      Country = " Chinese " 
3  
4      DEF  __init__ (Self, name, Age):
 5          self.name = name
 6          self.age = Age
 7  
8      DEF show_info (Self):
 9          Print ( " My Name: % s, Age: D% " % (the self.name, self.age))
 10  
. 11      @classmethod
 12 is      DEF show_country (CLS):
 13 is          Print (cls.country)
 14  
15  
16 P1 = the Person (" Xixi " ,. 6 )
 . 17  
18 is setattr (P1, " Age " ,. 7 )
 . 19  Print (p1.age)   # . 7 
20 is  
21 is setattr (P1, " Friend " , " haha " )
 22 is  Print (p1.friend)   # haha 
23  
24- setattr (the Person, " Country " , " People's Republic of China " )
 25  Print (Person.country)   # People's Republic of China
26 
27 setattr(Person, "color", "")
28 print(Person.color)  #
29 print(p1.color)  #

  4、delattr()

    Function: delete an object / class of property  
    Return Value: None

    delattr (object attribute name)
    delattr (class, attribute name)

    To delete the attribute does not exist, an error AttributeError: color

1  class the Person:
 2      Country = " Chinese " 
3  
4      DEF  __init__ (Self, name, Age):
 5          self.name = name
 6          self.age = Age
 7  
8      DEF show_info (Self):
 9          Print ( " My Name: % s, Age: D% " % (the self.name, self.age))
 10  
. 11      @classmethod
 12 is      DEF show_country (CLS):
 13 is          Print (cls.country)
 14  
15  
16 P1 = the Person ("xixi", 6)
17 
18 delattr(p1, "age")
19 # print(p1.age)  # AttributeError: 'Person' object has no attribute 'age'
20 
21 delattr(Person, "country")
22 # print(Person.country)  # AttributeError: type object 'Person' has no attribute 'country'
23 # print(p1.country)  # AttributeError: 'Person' object has no attribute 'country'
24 
25 # delattr(p1, "color")  # AttributeError: color

Guess you like

Origin www.cnblogs.com/Tree0108/p/12112961.html