zoukankan      html  css  js  c++  java
  • 内置函数(面向对象)

    这里我会只针对类和对象常规会用到的以下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 Filespython.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 Filespython.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 Filespython.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 Filespython.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 Filespython.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 Filespython.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 Filespython.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 Filespython.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 Filespython.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 Filespython.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
    
  • 相关阅读:
    wpf中显示HTML(转自http://steeven.cnblogs.com/archive/2006/06/12/424258.html)
    【msdn wpf forum翻译】TextBox中文本 中对齐 的方法
    【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?
    《Applications=Code+Markup》读书笔记 1(第一章 初识Application和Window)
    sql 分页
    Api
    快钱接口
    c#经典面试题
    static/const/readonly
    静态构造函数和静态类
  • 原文地址:https://www.cnblogs.com/lijian-22huxiaoshan/p/7133023.html
Copyright © 2011-2022 走看看