python反射技术

class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def __str__(self):
        return "Point({},{})".format(self.x,self.y)
    def show(self):
        print(self)

p1 = Point(4,5)
print(p1)
setattr(p1,'x',8)
print(p1)
print(getattr(p1,'x'))
print(getattr(p1,'__dict__'))
反射内建函数

getattr(object,name[,default])
setattr(object,name,value)
hasattr(object,name)

反射魔术方法

getattr()
setattr()
delattr()

你可能感兴趣的:(python反射技术)