python面向对象 反射原理解析
作者:宋鹏超 发布时间:2021-05-14 08:56:32
一、静态方法(staticmethod)和类方法(classmethod)
类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属×××互(也就是可以使用类属性)
静态方法:让类里的方法直接被类调用,就像正常调用函数一样
类方法和静态方法的相同点:都可以直接被类调用,不需要实例化
类方法和静态方法的不同点:
类方法必须有一个cls参数表示这个类,可以使用类属性
静态方法不需要参数
绑定方法:分为普通方法和类方法
普通方法:默认有一个self对象传进来,并且只能被对象调用-------绑定到对象
类方法:默认有一个cls对象传进来,并且可以被类和对象(不推荐)调用-----绑定到类
非绑定方法:静态方法:没有设置默认参数,并且可以被类和对象(不推荐)调用-----非绑定
import time
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
# @staticmethod
# def now():
# t=time.localtime()
# return Date(t.tm_year,t.tm_mon,t.tm_mday)
@classmethod #改成类方法
def now(cls):
t=time.localtime()
return cls(t.tm_year,t.tm_mon,t.tm_mday) #哪个类来调用,即用哪个类cls来实例化
class EuroDate(Date):
def __str__(self):
return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)
e=EuroDate.now()
print(e) #我们想触发EuroDate.__str__,此时e就是由EuroDate产生的,结果如我们所愿
'''
输出结果:
year:2017 month:3 day:3
'''
二、反射
反射:可以用字符串的方式去访问对象的属性,调用对象的方法(但是不能去访问方法),python中一切皆对象,都可以使用反射。
反射有四种方法:
hasattr:hasattr(object,name)判断一个对象是否有name属性或者name方法。有就返回True,没有就返回False
getattr:获取对象的属性或者方法,如果存在则打印出来。hasattr和getattr配套使用
需要注意的是,如果返回的是对象的方法,返回出来的是对象的内存地址,如果需要运行这个方法,可以在后面添加一对()
setattr:给对象的属性赋值,若属性不存在,先创建后赋值
delattr:删除该对象指定的一个属性
1、对象应用反射
class Foo:
def __init__(self):
self.name = 'egon'
self.age = 51
def func(self):
print('hello')
egg = Foo()
print(hasattr(egg,'name')) #先判断name在egg里面存在不存在,结果是True
print(getattr(egg,'name')) #如果为True它才去得到,结果是egon
print(hasattr(egg,'func')) #结果是True
print(getattr(egg,'func')) #得到的是地址<bound method Foo.func of <__main__.Foo object at 0x0000000001DDA2E8>>
getattr(egg,'func')() #在这里加括号才能得到,因为func是方法,结果是hello
一般用法如下,先判断是否hasattr,然后取getattr
if hasattr(egg,'func'):
getattr(egg,'func')() #结果是hello
else:
print('没找到')
2、类应用反射
class Foo:
f = 123
@classmethod
def class_method_dome(cls):
print('class_method_dome')
@staticmethod
def static_method_dome():
print('static_method_dome')
print(hasattr(Foo,'class_method_dome')) #结果是True
method = getattr(Foo,'class_method_dome')
method() #结果是class_method_dome
print(hasattr(Foo,'static_method_dome')) #结果是True
method1 = getattr(Foo,'static_method_dome')
method1() #结果是static_method_dome
3、模块应用反射
# 1.导入其他模块引用
import mymodule
print(hasattr(mymodule,'test'))
getattr(mymodule,'test')()
p = getattr(mymodule,'test')
p() #相当于上面getattr(mymodule,'test')()
# 2.在本模块中应用反射
def demo1():
print('hello')
import sys
module_obj = sys.modules[__name__] #相当于'__main__'
print(module_obj) #结果是<module '__main__' from 'C:/Users/Administrator/Desktop/test.py'>
print(hasattr(module_obj,'demo1')) #结果是True
getattr(module_obj,'demo1')() #结果是hello
导入自己的模块的例子:
def 注册():
print('regiester')
def 登录():
print('login')
def 购物():
pass
print('注册,登录,购物')
ret = input('请输入你要做的操作:')
import sys
my_module = sys.modules[__name__] #利用sys模块导入一个自己的模块
if hasattr(my_module,ret):
getattr(my_module,ret)()
来源:https://blog.51cto.com/qidian510/2161047


猜你喜欢
- 前言看到这里已经学习了创建各种 Python 数据类型的值。并且显示的值都是文字或常量值。>>> print(9.98)9
- k8s容器互联-flannel host-gw原理篇容器系列文章容器系列视频简析host-gw前面分析了flannel vxlan模式进行容
- 某天写代码突然县道这个问题,顺势总结一波JavaScript 函数和变量声明的“提前”(hoist)行为简单的说 如果我们使用 匿名函数va
- 定义和用法strftime() 函数根据区域设置格式化本地时间/日期。语法strftime(format,timestamp)参数 描述 f
- TTS简介TTS(Text To Speech)是一种语音合成技术,可以让机器将输入文本以语音的方式播放出来,实现机器说话的效果。TTS分成
- PyQt5切换按钮控件QPushButton简介QAbstractButton类为抽象类,不能实例化,必须由其他的按钮类继承QAbstrac
- 一、下载下载链接:https://www.anaconda.com/二、安装过程安装过程,所有都选默认项目。三、系统环境配置路径:此电脑-属
- 依赖os、sys、requests工具代码废话不多说,上代码。#!/usr/bin/env python3# -*- coding: utf
- <?php // fix 404 pages: header('HTTP/1.1 200 OK'); // set 4
- 下面的asp函数实现了对站点的所有缓存Application的清理,释放!Sub RemoveAllCache() D
- Python内置函数1. classmethod、staticmethod、property 。上述三个内置函数在文章(Python进阶——
- 目录HDFS NameNode 高可用Hadoop Namenode 高可用架构Namenode 高可用的实现隔离(Fencing)QJM共
- 字典的常用方法方便举例,先创建2个字典list_test={"bob":19,"aoa":18,&q
- ResNet沿用VGG完整的KaTeX parse error: Undefined control sequence: \time at
- 什么是计算属性概念计算属性是vue里面为了简化在模板语法中对响应式属性做计算而存在的什么时候应该使用计算属性根据现有响应式的值得到一个新的值
- 基本思路1、创建vueRouter,用公共路由实例化2、创建需要根据权限筛选的路由对象(在路由对象,添加必要的权限判断字段)3、登录完成,由
- Go 处理 json数据主要就是使用 json 包下的 Marshal 和 UnMarshal 两个函数。定义结构体 Usertype Us
- 1. 内置下载器中间件顺序{'scrapy.downloadermiddlewares.ajaxcrawl.AjaxCrawlMid
- 一、数据合并与分割1.tf.concat()填入两个tensor, 指定某维度,在指定的维度合并。除了合并的维度之外,其他的维度必须相等。2
- 版本:python2.7 2.7 2.7!!!症状:比如,我编写了一个字符串number,输出到网页上,变成了u'number