Python笔记【7】_反射getattr&hasattr&setattr&delattr
2024-09-02 00:50:14
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)
最新文章
- C++ 代码优化
- jquery + header
- Silverlight类百度文库在线文档阅读器
- Android判断网路是否畅通加权限
- Apache2.4权限配置(原创帖-转载请注明出处)
- 关于 XMLHttpRequest对象的onreadyStateChange方法
- loadrunner关联和事务
- IOS-UITextField键盘不隐藏问题
- 安装Office 2010后出现正在配置
- Lining.js - 为CSS提供 ::nth-Line 选择器功能
- OSWatcher Black Box
- ubutu之jdk安装
- 安装courier-authlib找不到mysqlclient.so文件
- IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践
- Java Script基础(二) 基本语法
- C# 类与接口的几个问题的总结(待续)
- 使用commons-fileupload进行上传
- canvas动画——粒子系统(1)
- 28、初识socket(subprocess模块)
- MongoDB学习总结(二)
热门文章
- HTML5 随手记(4)
- ASP.NET Core 异常和错误处理 - ASP.NET Core 基础教程 - 简单教程,简单编程
- Android Studio怎样提示函数使用方法
- Qt如何绘制(简单深刻,fearlazy的系列文章)
- Android发展_备份短信
- MySQL第五个学习笔记 该数据表的操作
- 汉顺平html5课程分享:6小时制作经典的坦克大战!
- intel dpdk IPv4 Fragmentation Sample Application 测试
- 最简单的IdentityServer实现——IdentityServer
- glibc内存管理方式