网站首页 > 技术文章 正文
1.动态属性访问
class TestClass:
def __init__(self):
self.name1 = 'test1'
self.name2 = 'test122'
obj = TestClass()
test_name = 'name1'
print(getattr(obj, test_name)) # 输出: test1 //获取属性名称为name1的值
setattr(obj, test_name, 'new_test123')//重新设置属性名称名称为name1的值
print(getattr(obj, test_name)) # 输出: new_test123
2.动态方法调用
drg源码的实现方式类似这种方式
class Animal(object):
def __init__(self, name, age, init_func_str):
self.name = name
self.age = age
func = getattr(self, init_func_str)
func()
def sleep(self):
print("sleeping...")
class Dog(Animal):
def wangwang(self):
print("旺旺叫")
def sleep(self):
print("仰天睡...")
d = Dog("alex", 23, "sleep")
猜你喜欢
- 2025-04-30 深入探究Python中`__init__.py`文件的奥秘
- 2025-04-30 Python基础教程 第6课 Input()函数的使用
- 2025-04-30 python中os模块的12种用法(python os.path模块)
- 2025-04-30 10 个鲜为人知的 Python 可视化概念和技巧
- 2025-04-30 带你了解python 中global的用法(python中globals用法)
- 2025-04-30 python中嵌套使用,及如何避免嵌套地狱
- 2025-04-30 Python高级特性揭秘:14个鲜为人知的编程秘籍
- 2025-04-30 零基础学Python!你准备好了吗?第一章·第七课 变量的用处!
- 2025-04-30 掌握 Python:基本语法(python基本语法总结)
- 2025-04-30 python中range用法详解(python语言range用法)
- 07-01Python列表创建操作与遍历指南(用python创建一个列表)
- 07-01Python语言学习实战-内置函数filter()的使用(附源码)
- 07-01Python基础编程20例之七:filter过滤,筛选奇数
- 07-01使用Python过滤指定进程的技巧(python中怎么删除列表中指定的值)
- 07-01Python列表推导式如何过滤偶数?(python过滤列表中的所有负数)
- 07-01豆瓣评分8.6 《零基础入门学习Python》零基础小白必备 建议收藏
- 07-01送PDF书 | 豆瓣9.2分,超250万Python新手的选择!蟒蛇书入门到实践
- 07-01Python 从底层结构Beautiful Soup 4(内置豆瓣电影排行爬取案例)
- 272℃Python短文,Python中的嵌套条件语句(六)
- 271℃python笔记:for循环嵌套。end=""的作用,图形打印
- 270℃PythonNet:实现Python与.Net代码相互调用!
- 264℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 264℃Python实现字符串小写转大写并写入文件
- 123℃原来2025是完美的平方年,一起探索六种平方的算吧
- 105℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 101℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- Python列表创建操作与遍历指南(用python创建一个列表)
- Python语言学习实战-内置函数filter()的使用(附源码)
- Python基础编程20例之七:filter过滤,筛选奇数
- 使用Python过滤指定进程的技巧(python中怎么删除列表中指定的值)
- Python列表推导式如何过滤偶数?(python过滤列表中的所有负数)
- 豆瓣评分8.6 《零基础入门学习Python》零基础小白必备 建议收藏
- 送PDF书 | 豆瓣9.2分,超250万Python新手的选择!蟒蛇书入门到实践
- Python 从底层结构Beautiful Soup 4(内置豆瓣电影排行爬取案例)
- python实现爬取豆瓣电影Top250(python爬取豆瓣电影的流程)
- 基于Python+Flask+SQLite的豆瓣电影可视化系统
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)