python-Object-oriented reflection

Source: Internet
Author: User

Reflection

The dynamic assembly is implemented, and the properties and methods in the class are reflected through the string.

Yi, hasarttr (obj,name_str)

Function: Determines whether an object in obj has an attribute or method of the corresponding NAME_STR string.

Class Dog (object):    def __init__ (self, name):        self.name = name    def eat (self, food):        print ('%s is eating% S "% (self.name, food)) d = Dog (" Tom ") d.eat (' Apple ') choice = input (" >>>: "). Strip () print (hasattr (d, Choice))  # If there is a choice string property or method # # output # >>>:name  # Input object exists in obj property # true# >>>:eat  # Input object exists method # True

Second, GetAttr (OBJ,NAME_STR)

Function: Gets the memory address of the corresponding method in the Obj object or the value of the corresponding property according to the string name_str.

Class Dog (object):    def __init__ (self, name):        self.name = name    def eat (self, food):        print ('%s is eating% S "% (self.name, food)) d = Dog (" Tom ") d.eat (' Apple ') choice = input (" >>>: "). Strip () print (GetAttr (d, Choice))    # Get the memory address of the corresponding method in the D object or the value of the corresponding property # >>>:name  # Gets the value of the input object # tom# >>>:eat   # Get the method of the input object # <bound Method Dog.eat of <__main__. Dog Object at 0x000002a4adb732e8>>

Iii. setattr (x, Y, z)

Function: Adds a new property or new method to the Obj object,setattr (x, ' Y ', V) is equivalent to ' x.y = V '.

1. Add a new method to the object

def bulk (self):  # First define a bulk function    print ("%s is yelling ..."% self.name) class Dog (object):    def __init__ (self, Name):        self.name = name    def eat (self, food):        print ('%s is eating%s '% (self.name, food)) d = Dog ("Tom") choice = Input (">>>:"). Strip () SetAttr (d, Choice, bulk)  # input is talk, so it is equivalent to D.talk = bulk# D.talk (d) Direct write Dead, with D.talk (d) , generally not so write Func = GetAttr (d, Choice)  # with GetAttr to get Func (d) # output # >>>:talk# Tom is yelling ...

2. Add a property to the object

Class Dog (object):    def __init__ (self, name):        self.name = name    def eat (self, food):        print ('%s is eating% S "% (self.name, food)) d = Dog (" Tom ") Choice = input (" >>>: "). Strip () SetAttr (d, Choice, ' Apple ')  # Food, So it's equivalent to D.food = ' Apple ' # print (D.food) so it's dead, or with one of the following print (GetAttr (d, choice)) # output # >>>:food# Apple

Iv. delattr (x, y)

Function: Delete the property or method in the Obj object,delattr (x, ' Y ') is equivalent to ' del x.y ' .

Class Dog (object):    def __init__ (self, name):        self.name = name    def eat (self, food):        print ('%s is eating %s "% (self.name, food)) d = Dog (" Tom ") Choice = input (" >>>: "). Strip () delattr (d, Choice)  # Delete an attribute or method print (d.name) print (d.eat) based on a string

V. Comprehensive use of hasattr, GetAttr, SetAttr

Class Dog (object):    def __init__ (self, name):        self.name = name    def eat (self, food):        print ('%s is eating% S "% (self.name, food)) d = Dog (" Tom ") Choice = input (" >>>: "). Strip () if Hasattr (d, choice):  # Determine the existence of properties and methods in the D object C5/>name_value = GetAttr (d, Choice)  # Get property value    print (name_value)    setattr (d, Choice, "Jerry")  # Modify Property values    Print (GetAttr (d, Choice))  # re-get the value of the property else:    SetAttr (d, Choice, None)  # Sets the value of the property that does not exist    v = GetAttr (d, Choice)    print (v)

python-Object-oriented reflection

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.