1.反射

hasattr getattr delattr setattr

优点:事先定义好接口,接口只有在被完成后才能真正执行,这实现了即插即用,这其实是一种“后期绑定”,即先定义好接口,

然后是再去实现具体的功能

print(hasattr(p, 'age'))  # True 是否有属性 判断

print(getattr(p, 'name', '没有找到该参数'))  # get属性值
print(getattr(p, 'name1', 'False')) # False setattr(p, 'name', 'sb') # 设置属性
print(p.__dict__) delattr(p, 'name') # 删除属性
print(p.__dict__)
setattr(p, 'func', lambda x: x + 1)
setattr(p, 'func1', lambda self: self.name + " sb")
print(p.__dict__)
print(p.func(100)) # 添加函数属性
setattr(p, 'name', 'wangwu')
print(p.func1(p))
# 反射应用场景
class FtpClient:
#'ftp客户端,但是还么有实现具体的功能'
def __init__(self, addr):
print('正在连接服务器[%s]' % addr)
self.addr = addr from module import FtpClient
f1 = FtpClient('192.168.1.1')
if hasattr(f1, 'get'): # 判断f1是否有get方法 如果没有 就执行else语句
func_get = getattr(f1, 'get')
func_get()
else:
print('---->不存在此方法')
print('处理其他的逻辑')

2.动态导入模块

# 动态导入模块
module__test = __import__('macboy.wsgi') # 此处就是macboy位置
print(module__test) # <module 'macboy' from 'C:\\Disk_D\\pycharm_stu\\macboy\\macboy'> import importlib m = importlib.import_module('macboy.wsgi') # 此处是macboy.wsgi位置
print(m) # <module 'macboy.wsgi' from 'C:\\Disk_D\\pycharm_stu\\macboy\\macboy\\wsgi.py'>

3.类attr属性 __gerattr__ __setattr__ __delattr__ __getattribute__

# 双下划线开头的类属性 obj.属性 触发
class Foo:
def __init__(self, name):
self.name = name # 属性不存在时 被触发 比较常用
def __getattr__(self, item):
return "%s属性不存在" % (item) # 添加属性时 被触发
def __setattr__(self, key, value):
self.__dict__[key] = str(value) # 删除属性时 被触发
def __delattr__(self, item):
self.__dict__.pop(item) a = Foo('wangwu')
print(a.name)
print(a.ww) # ww属性不存在
# print(a.__dict__) a.age = 10
a.gender = 'man'
# print(a.__dict__)
print(dir(Foo))
# print(Foo.__dict__) print(a.gender) # "man"
print(a.age) #
# print(a.__dict__) del a.gender
del a.age print(a.gender) # gender属性不存在
print(a.age) # age属性不存在 # __getattribute 和 __getarr__ 不管找不找的到,都会执行 先前面执行后面后执行 不管是否有偶异常 __getattribute都执行
# 当__getattribute抛出一异常 小弟__getarr__接受到 处理异常
class Foo:
    def __init__(self, name):
        self.name = name     def __getattr__(self, item):
        return "你找的%s属性不存在" % (item)     def __getattribute__(self, item):
        print("执行的是__getattribute")
        raise AttributeError("抛出异常") f1 = Foo('fdsf')
print(f1.fdd)

4.继承方式包装

包装:python为大家提供了标准数据类型,以及丰富的内置方法,其实在很多场景下我们都需要基于标准数据类型来定制我们自己的数据类型,新增/改写方法,这就用到了我们刚学的继承/派生知识(其他的标准类型均可以通过下面的方式进行二次加工)

#  二次加工标准类型(包装)
class List(list):
def append(self, item):
if isinstance(item, str):
super().append(item)
else:
print("只能加字符串") l = List("helloworld")
l.append("")
l.append(111) # 只能加字符串 print(l)

5.组合方式授权

授权:授权是包装的一个特性, 包装一个类型通常是对已存在的类型的一些定制,这种做法可以新建,修改或删除原有产品的功能。其它的则保持原样。授权的过程,即是所有更新的功能都是由新类的某部分来处理,但已存在的功能就授权给对象的默认属性。

实现授权的关键点就是覆盖__getattr__方法

# 组合方式授权
import time
class Open:
def __init__(self, filename, mode='r', encoding='utf-8'):
self.file = open(filename, mode, encoding=encoding)
self.mode = mode
self.encoding = encoding def write(self, line):
t = time.strftime("%Y-%m-%d %X")
self.file.write('%s %s' % (t, line)) def __getattr__(self, item):
# >>> getattr(list,"append")
# <method 'append' of 'list' objects>
return getattr(self.file, item) # file中执行item方法 f1 = Open('a.txt', 'w+')
print(f1.__dict__)
print(dir(f1.file)) f1.write('内存空间不足\n')
f1.write('服务器超载\n')
f1.seek(0)
print(f1.read())

python 反射 动态导入模块 类attr属性的更多相关文章

  1. Python importlib(动态导入模块)

    使用 Python importlib(动态导入模块) 可以将字符串型的模块名导入 示例: import importlib module = 'module name' # 字符串型模块名 test ...

  2. python importlib动态导入模块

    一般而言,当我们需要某些功能的模块时(无论是内置模块或自定义功能的模块),可以通过import module 或者 from * import module的方式导入,这属于静态导入,很容易理解. 而 ...

  3. python_反射:动态导入模块

    官方推荐方法: test_mod.py def hi(): print('Hi') test.py import importlib q = importlib.import_module('test ...

  4. python中动态导入模块

    当导入的模块不存在时,就会报ImportError错误,为了避免这种错误可以备选其他的模块或者希望优先使用某个模块或包,可以使用try...except...导入模块或包的方式. 例如: Python ...

  5. java利用反射动态获取实体类的属性值

    直接贴代码吧,有需要的话,可以根据自己的需要修改部分代码: public BigDecimal getByAttributeName(ThmdGwqriR thmdGwqriR, String att ...

  6. python动态导入模块——importlib

    当在写代码时,我们希望能够根据传入的选项设置,如args.model来确定要导入使用的是哪个model.py文件,而不是一股脑地导入 这种时候就需要用上python的动态导入模块 比如此时文件结构为: ...

  7. Python 实现接口类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)

    实现接口类的两种方式 方式一 from abc import ABCMeta from abc import abstractmethod class BaseMessage(metaclass=AB ...

  8. python面向对象反射-框架原理-动态导入-元类-自定义类-单例模式-项目的生命周期-05

    反射 reflect 反射(reflect)其实是反省,自省的意思 反省:指的是一个对象应该具备可以检测.修改.增加自身属性的能力 反射:通过字符串获取对象或者类的属性,进行操作 设计框架时需要通过反 ...

  9. Python 实现抽象类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)

    实现抽象类的两种方式 方式一 from abc import ABCMeta from abc import abstractmethod class BaseMessage(metaclass=AB ...

随机推荐

  1. Windows7上用VS编译本地使用的live555

    本文链接:https://www.jianshu.com/p/6ea100865744 环境 系统:Windows7 SP1 64位 编辑器:Visual Studio Community 2017 ...

  2. SpringBoot 整合 Logback

    Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.L ...

  3. vue下拉搜索

    vue版本是1.0.21 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  4. WPF中Label使用StringFormat

    1. 在WPF中Label的Content有时内容只需要改变个别数字,而不需要所以内容都修改,这时候就要使用StringFormat, 如: <Label Content="I hav ...

  5. HDU 1102 Constructing Roads(kruskal)

    Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...

  6. Delphi下OpenGL2d绘图(05)-画图片Bmp

    一.前言 找了不少资料,要画图片要先处理一下,需要引用别的单元,Delphi中没带,需要另外下载Gl.pas.看网上说是自带的OpenGl单元封装的是1.0版的,有此函数未声明.网上可以找到Gl.pa ...

  7. jsonp/ajax 自己的一些总结

    data.json代码:[{"name": "张三", "age": 18}, {"name": "李四&qu ...

  8. 如何快速备份还原Sql Server 数据库

    备份数据库 选择你要备份的数据库,鼠标右键单击,选择任务-备份 弹出备份数据库窗口,选择添加 弹出选择备份目标窗口,点击浏览,选择存放备份数据库的目录,输入文件名,后缀名输入.bak,点击确定,确定, ...

  9. Unity 动态加载资源的方式。

    方式 特点  用法  Resource.load  安装包会比较大  在Asset文件夹下建一个Resources命名的文件夹,在打包安装包时会把 Resources文件夹下的所有文件都打包进去,不管 ...

  10. 如何把连接字符串放到App.cfg配置文件中

    首先主Windows程序要添加一个[应用程序配置文件]会在项目中生成一个App.config文件. 在项目中也引用System.Configuration这个引用 当前文档中没有源. 在后台数据类库中 ...