zoukankan      html  css  js  c++  java
  • Python笔记【7】_反射getattr&hasattr&setattr&delattr

    Lesson0402_GetatrrWebsite.py

    #!/usr/bin/env/python
    #-*-coding:utf-8-*-
    
    #Author:LingChongShi   #查看源码Ctrl+左键
    
    def index():
        print('欢迎访问XX网站')
    
    def login():
        print('登录成功')
    
    def logout():
        print('退出登录')
    
    class People(object):
        country='China'
        def __init__(self):
            pass
        def people_info(self):
            print('People类中people_info函数')
    Lesson0403_Getattr.py
    #!/usr/bin/env/python
    #-*-coding:utf-8-*-
    
    #Author:LingChongShi   #查看源码Ctrl+左键
    
    '''
    getattr():根据字符串的形式去某个模块中查找X函数
    hasattr():根据字符串的形式去某个模块判断X函数是否存在
    setattr():根据字符串的形式去某个模块设置X函数
    delattr():根据字符串的形式去某个模块删除X函数
    '''
    import Lesson04_Package.Lesson0402_GetatrrWebsite
    '''getattr(object,name,default):
    1、object:对象(模块)
    2、name:属性(函数/方法)
    3、default:无对应属性,返回的值,
    4、有对应属性,返回对象属性值
    '''
    getder=getattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'index','-1')
    print(getder)
    getder()
    
    obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People()
    getclass=getattr(obj,'people_info','-1')
    getclass()
    
    '''hasattr(object,name):
    1、object:对象(模块)
    2、name:属性(函数/方法)
    3、如果对象有该属性返回True,否则返回False
    '''
    has=hasattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'login')
    print(has)
    
    obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People()
    hasclass=hasattr(obj,'people_info')
    print(hasclass)
    
    '''setattr(object,name,value):
    1、object:对象(模块)
    2、name:属性(函数/方法)
    3、value:属性值
    4、无返回值
    '''
    set=setattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'str','添加的字符串')
    has1=hasattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'str')
    print(has1)
    get1=getattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'str')
    print(get1)
    
    obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People()
    setclass=setattr(obj,'exit','退出')
    hascalss=hasattr(obj,'exit')
    print(hasclass)
    
    '''delattr(object,name):
    1、object:对象(模块)
    2、name:属性(函数/方法)
    3、无返回值
    '''
    del1=delattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'logout')
    has2=hasattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'logout')
    print(has2)
    # get2=getattr(Lesson04_Package.Lesson0402_GetatrrWebsite,'logout')
    # print(get2)
    
    obj=Lesson04_Package.Lesson0402_GetatrrWebsite.People
    hasclass=hasattr(obj,'people_info')
    print(hasclass)
    delclass=delattr(obj,'people_info')
    hasclass=hasattr(obj,'people_info')
    print(hasclass)
  • 相关阅读:
    福利贴——爬取美女图片的Java爬虫小程序代码
    select多选 multiple的使用
    Android笔记---点击事件的四种写法
    二叉排序树的插入与删除
    hdu 5269 ZYB loves Xor I && BestCoder Round #44
    linux 下同步异步,堵塞非堵塞的一些想法
    JavaScript编程随笔
    《从零開始学Swift》学习笔记(Day 51)——扩展构造函数
    What's Wrong With Hue Oozie Editor?
    2015.7个人反思小结以及兴许规划
  • 原文地址:https://www.cnblogs.com/sjl179947253/p/10282081.html
Copyright © 2011-2022 走看看