Python放射的代码实现


本文摘自php中文网,作者零到壹度,侵删。

这篇文章介绍的内容是Python放射的代码实现,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

'''

放射

hasattr(obj, name_str):判断一个对象obj里是否有对应的name_str字符串的方法

getattr(obj, name_str):根据name_str字符串去获取obj对象里的对应的方法的内存地址

'''

def bulk(self):

print("%s is yelling..." % self.name)

class People(object):

def __init__(self, name):

self.name = name

def talk(self):

print("%s is talking..." % self.name)

User = People("UserPython")

choice = input(">>>:")

# 判断一个对象User里是否有对应的choic = talk字符串的方法

# print(hasattr(User, choice)) #True

# 根据choice字符串去获取User对象里的对应的方法的内存地址

# print(getattr(User, choice)) #<bound method People.talk of <__main__.People object at 0x0000000002741208>>

if hasattr(User, choice):

func = getattr(User, choice)

func()

else:

setattr(User, choice, bulk)

User.bulk(User)

相关推荐:

Python放射的代码实例

python反射

Python--反射/自省

以上就是Python放射的代码实现的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python中divmod是什么

Python就用anaconda神器

Python中reverse、sort、sorted三个列表排序使用方法详解

Python中“//”表示什么意思

Python怎么把三位数拆开

一起看看Python常用字符串及其操作

如何利用Python合并文本

Python需要什么基础

Python中if语句用法

基于Python的2d图库matplotlib的图例、标题和标签介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...