Object-oriented-reflection of Python

Source: Internet
Author: User
Tags reflection

First, what is reflection

The concept of reflection was first proposed by Smith in 1982, mainly referring to the ability (introspection) of a program to access, detect, and modify its state or behavior in the province. The proposal of this concept soon triggered the research on the application of reflectivity in Computer science field. It is first used in the field of programming language design, and has achieved achievements in Lisp and object-oriented.

Python-oriented reflection in objects: manipulating Object-related properties in the form of strings. Everything in Pythonn is an object (you can use reflection)

Four methods of reflection

Everything is object, and the class itself is an object

Hasattr
def hasattr(*args, **kwargs): # real signature unknown    """    Return whether the object has an attribute with the given name.        This is done by calling getattr(obj, name) and catching AttributeError.    """    pass
GetAttr
def getattr(object, name, default=None): # known special case of getattr    """    getattr(object, name[, default]) -> value        Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.    When a default argument is given, it is returned when the attribute doesn't    exist; without it, an exception is raised in that case.    """    pass
SetAttr
def setattr(x, y, v): # real signature unknown; restored from __doc__    """    Sets the named attribute on the given object to the specified value.        setattr(x, 'y', v) is equivalent to ``x.y = v''    """    pass
Delattr
def delattr(x, y): # real signature unknown; restored from __doc__    """    Deletes the named attribute from the given object.        delattr(x, 'y') is equivalent to ``del x.y''    """    pass
Third, the case
class Foo:    f = '类的静态变量'    def __init__(self,name,age):        self.name=name        self.age=age    def say_hi(self):        print('hi,%s'%self.name)obj=Foo('john',73)#检测是否含有某属性print(hasattr(obj,'name'))print(hasattr(obj,'say_hi'))#获取属性n=getattr(obj,'name')print(n)func=getattr(obj,'say_hi')func()print(getattr(obj,'aaaaaaaa','不存在啊')) #报错#设置属性setattr(obj,'sb',True)setattr(obj,'show_name',lambda self:self.name+'sb')print(obj.__dict__)print(obj.show_name(obj))#删除属性delattr(obj,'age')delattr(obj,'show_name')delattr(obj,'show_name111')#不存在,则报错print(obj.__dict__)

Object-oriented-reflection of Python

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.