python-魔法属性和反射
python魔法属性和反射
#!/usr/bin/python3
# coding:utf-8
# Auther:AlphaPanda
# Description:与类相关的魔法属性
# Version:1
# Date:Wed Dec 4 02:22:28 EST 2019 class Man():
pass class Woman():
pass class Children(Man,Woman):
"""
成员属性:
skin,hair
成员方法:
eat,drink,__sleep
"""
skin = "黑皮肤"
hair = "黄头发" # 普通无参方法
def eat():
print("小孩会吃糖果")
# 绑定方法:
def drink(self):
print("小孩会吃奶奶")
# 大笑
def laugh(self,func):
print(func)
# 获取函数的名字
print(func.__name__)
print("小孩会大笑")
# 私有方法
def __sleep(self):
print("小孩喜欢睡觉")
# __dict__ 获取对象或类的内部成员结构
obj = Children()
print(obj.__dict__)
print(Children.__dict__) # __doc__ 获取对象或者类的内部文档
print(obj.__doc__)
print(Children.__doc__) # __name__ 获取类名函数名
def ceshi():
print("我是测试函数")
obj.laugh(ceshi) # __class__ 获取当前对象所属的类
cls = obj.__class__
print(cls.hair) # __bases__ 获取一个类直接继承的所有父类,返回元组
tup = Children.__bases__
print(tup) ### 反射 :通过字符串去操作类对象,或者模块中的属性方法
# 类的反射
# 1 hasattr() 检测对象/类中是否有指定成员
# 对象
res = hasattr(obj,"skin") # 属性
print(res)
res = hasattr(obj,"drink") # 方法
print(res) # 类
res = hasattr(Children,"eat")
print(res) # 2 getaddr() 获取对象/类成员的值 # 对象
res = getattr(obj,"hair") # 属性
print(res) res = getattr(obj,"hair123","第三个参数是默认值,如果该成员不存在,默认返回该值")
print(res) # 类:
res = getattr(Children,"drink")
print(res) # 反射对象当中的方法
func1 = getattr(obj,"drink")
print(func1) # 反射的是绑定到对象的方法,对象系统会自动传递
func1() # 反射类当中的方法
func2 = getattr(Children,"drink")
print(func2)
func2(2) """
func = input("请输入您要反射的行数:")
if hasattr(Children,func):
func_new = getattr(Children,func)
func_new()
else:
print("不存在该方法")
"""
# 3 setattr()设置对象/类成员的值
# 对象
setattr(obj,"name","wangwen")
print(obj.name) # 类:
setattr(Children,"wc",lambda : print("小孩会尿尿"))
print(Children.__dict__)
Children.wc() # 4 delattr() 删除对象/类成员的值
# 对象
print(obj.__dict__)
delattr(obj,"name")
print(obj.__dict__) # (2) 模块的反射
"""
包:文件夹
模块:文件
"""
def func1():
print("我是func1方法")
def func2():
print("我是func2方法")
def func3():
print("我是func3方法")
def func4():
print("我是func4方法") # sys.modules 返回一个字典,字典的键是加载的所有模块
import sys
print(sys.modules)
# 获取当前模块得对象,通过这个对象可以进行反射 __main__这个键对应的是本模块对象
mymodule = sys.modules["__main__"]
print(mymodule) # 小案例:通过字符串操作了该模块中相应的方法
while True:
strvar = input("请输入你要反射的方法:")
if hasattr(mymodule,strvar):
func = getattr(mymodule,strvar)
func()
else:
print("没有改函数")
python-魔法属性和反射的更多相关文章
- python魔法属性
1.__doc__:表示类的描述信息 class Person(object): '''定义人的类''' def func(self): pass print(Person.__doc__) 结果为: ...
- python进阶之类常用魔法方法和魔法属性
前言 前面我们总结过了python的关键字.运算符.内置函数.语法糖等与python魔法方法之间的关系,现在我们更细一点,看看python的面向对象编程有哪些常用的魔法属性和魔法方法. 魔法属性 对于 ...
- python进阶之函数和类内建魔法属性
前言 关于对象的魔法方法我们已经讲得太多,但是对于类或函数内建的魔法属性和功能我们涉及较少,下面系统了解一下类和函数的内建属性. 查看内建属性 class Person(object): pass d ...
- Python类属性访问的魔法方法
Python类属性访问的魔法方法: 1. __getattr__(self, name)- 定义当用户试图获取一个不存在的属性时的行为 2. __getattribute__(self, name)- ...
- Python高级笔记(七)魔法属性
1. 私有属性 名字重整 print(Test.__dict__) {'__weakref__': <attribute '__weakref__' of 'Test' objects>, ...
- 『无为则无心』Python面向对象 — 60、魔法属性
目录 1.魔法属性__name__ 2.魔法属性__bases__ 3.魔法属性__mro__ 4.魔法属性__doc__ 5.魔法属性__module__ 和__class__ 6.魔法属性__di ...
- Python魔法 - MetaClass
Python魔法 - MetaClass metaclass The class of a class. Class definitions create a class name, a class ...
- Python魔法方法总结及注意事项
1.何为魔法方法: Python中,一定要区分开函数和方法的含义: 1.函数:类外部定义的,跟类没有直接关系的:形式: def func(*argv): 2.方法:class内部定义的函数(对象的方法 ...
- python魔法方法:__getattr__,__setattr__,__getattribute__
python魔法方法:__getattr__,__setattr__,__getattribute__ 难得有时间看看书....静下心来好好的看了看Python..其实他真的没有自己最开始想的那么简单 ...
随机推荐
- Python学习之数据库
9.6 表的查询 [结构]select distinct 字段1,字段2 from 表名 where 条件 group by 字段 having 筛选 order by 字段 limit 限制条数 [ ...
- USACO2.1 Hamming Codes【枚举+二进制处理+输出格式+题意理解】
这道题加了2个看起来奇奇怪怪的$tag$ 1.输出格式:不得不说这个格式输出很恶心,很像$UVA$的风格,细节稍微处理不好就会出错. 因为这个还$WA$了一次: ,m=n; ) { ;i<=t+ ...
- mysql8无法用navicat连接(mysql8加密方式的坑)
关键词:mysql8无法用navicat连接,navicat无法连接mysql8,mysql8,mysql8的加密方式 [1]mysql8 的坑 密码加密规则 在MySQL 8.0.以上版本中,cac ...
- [转帖]2017年新闻: 中国CPU还在“群雄割据” ,印度已确定了国家指令集
中国CPU还在“群雄割据” ,印度已确定了国家指令集 时间:2017-12-21 作者:观察者网 https://www.eet-china.com/news/201712210610.html ...
- python3抓取中国天气网不同城市7天、15天实时数据
思路:1.根据city.txt文档来获取不同城市code2.获取中国天气网7d和15d不同城市url3.利用requests库请求url获取html内容4.利用beautifulsoup获取7d和15 ...
- python 序列 转换 各种操作
# 数据结构 字符串 列表 元组 数字序列# 10-19的整数# r1 = range(10,20)# print(r1)# print(type(r1))## # 19# print(r1[9])# ...
- golang 反射中调用方法
反射中调用函数 众所周知,golang中的函数是可以像普通的int.float等类型变量那样作为值的,例如: package main import "fmt" func hell ...
- Let和Const的使用
ES2015(ES6) 新增加了两个重要的 JavaScript 关键字: let 和 const. let 声明的变量只在 let 命令所在的代码块内有效,const 声明一个只读的常量,一旦声明, ...
- Haproxy学习总结
一.Haproxy介绍 1.实现了一种事件驱动,单一进程模型,支持数万计的并发连接,用于为tcp和http应用程序提供高可用,负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制 ...
- 2019-11-29-win10-uwp-如何开始写-uwp-程序
title author date CreateTime categories win10 uwp 如何开始写 uwp 程序 lindexi 2019-11-29 10:12:42 +0800 201 ...