getattr and @property
来源:互联网 发布:淘宝卖家多长时间回款 编辑:程序博客网 时间:2024/06/03 14:22
1. property的作用:
负责把一个方法变成属性调用
2. getattr(object, name[,default])
需要注意的是,如果是返回的对象的方法,返回的是方法的内存地址,如果需要运行这个方法,
可以在后面添加一对括号。
>>> class test():
... name="Wang"
...
... def run(self):
... return "Hello"
...
>>>t=test()
>>> t.run()
'Hello'
>>> t.run
<bound method test.run of <__main__.test instance at 0x105a64f80>>
>>> getattr(t, "run")()
'Hello'
>>> getattr(t, "run")
<bound method test.run of <__main__.test instance at 0x105a64fc8>>
如果使用@property
>>> class test():
... name="Wang"
... @property
... def run(self):
... return "Hello"
...
>>> t=test()
>>> t.run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> t.run
'Hello'
>>> getattr(t, "run")()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
'Hello'
- getattr and @property
- getattr
- getattr
- getattr
- @property and @synthesize
- Property and Synthesize
- Property and Outlet
- Javascript constructor and property
- python入门笔记(Day7)--slots,@property,MixIn,定制(str,iter,getitem,getattr,call)枚举(Enum)元type,metaclass,ORM
- Difference between Property and Indexer
- sequence and property operator summary
- Object-C Property and Synthesize
- getattr & descriptor
- Class getattr()
- getattr函数
- python getattr()
- python -- getattr
- python getattr
- Ubuntu12.04已有jre6后再安装jre8
- 安卓传感器SensorManager实现简单指南针
- 数据库连接池设计
- Android 6.0权限
- [Java]使用浏览器运行本地的applet
- getattr and @property
- 计算机图形学07一一 多边形理解 理解 奇偶规律 非零环绕数
- Android系统版本与代号
- Ubuntu 上安装截图工具 Shutter,及系统基本截图快捷键设置
- git 常用命令
- 解决lua中截取字符串(中英文混合)赋值失败的问题
- 使用百度地图遇到的坑
- Spring入门3——注解篇
- git 的基本使用