内置函数(面向对象)

这里我会只针对类和对象常规会用到的以下4个内置函数进行实例介绍

  • hasattr
  • getattr
  • setattr
  • delattr

下面是主体函数

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)

针对类

  • hasattr

判断school这个属性在不在Teacher这个类里返回值为布尔值

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
print(hasattr(Teacher,"school")) 

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
True

Process finished with exit code 0

  • getattr

取出school在Teacher中绑定的值

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
print(hasattr(Teacher,"school")) 

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
MIT

Process finished with exit code 0

同时getattr还有另外一种用法:即在可以查看类的属性指定一个返回值

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
None

Process finished with exit code 0

  • setattr

设定类的数据属性值

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)

Teacher.x=3
setattr(Teacher,"x",123)  
print(Teacher.x)

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
123

Process finished with exit code 0
  • delattr

删除类的数据属性

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
        
delattr(Teacher,"school")    
print(getattr(Teacher,"school",None))     

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
None

Process finished with exit code 0

针对对象

  • hasattr
class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
        
t=Teacher("whatmini",18)
print(hasattr(t,"name"))

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
True

Process finished with exit code 0
  • getatt
class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
t=Teacher("whatmini",18)
print(getattr(t,"name")) 

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
whatmini

Process finished with exit code 0
  • setattr
class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
        
setattr(t,"sex","male")  
print(getattr(t,"sex"))

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
male

Process finished with exit code 0
  • delattr
class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
print(t.__dict__)
delattr(t,"name")  
print(t.__dict__)

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
{'name': 'whatmini', 'age': 18}
{'age': 18}

Process finished with exit code 0

这里我演示的删除是在对象的字典中直接删除。需要明白的是这四个内置函数在对类和对象的属性进行的增删查该的行为,都是在类和对象的名称空间进行的,这是本质的行为。

class Teacher:
    school="MIT"
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def teach(self):
        print("%s teach" %self.name)
print(getattr(t,"teach"))
print(getattr(t,"school"))
t.school="kjkjkjkjkj"
print(t.__dict__)

"D:\Program Files\python.exe" "E:/py_code/day 12/反射.py"
<bound method Teacher.teach of <__main__.Teacher object at 0x00000000029420B8>>
MIT
{'name': 'whatmini', 'age': 18, 'school': 'kjkjkjkjkj'}

Process finished with exit code 0
posted @ 2017-07-07 16:48  Sun珊珊来迟  阅读(125)  评论(0编辑  收藏  举报