@classmethod, @staticmethod和@property这三个装饰器的使用对象是在类中定义的函数。下面的例子展示了它们的用法和行为:
2024-09-01 08:40:19
class MyClass(object):
def __init__(self):
self._some_property = "properties are nice"
self._some_other_property = "VERY nice"
def normal_method(*args,**kwargs):
print "calling normal_method({0},{1})".format(args,kwargs)
@classmethod
def class_method(*args,**kwargs):
print "calling class_method({0},{1})".format(args,kwargs)
@staticmethod
def static_method(*args,**kwargs):
print "calling static_method({0},{1})".format(args,kwargs)
@property
def some_property(self,*args,**kwargs):
print "calling some_property getter({0},{1},{2})".format(self,args,kwargs)
return self._some_property
@some_property.setter
def some_property(self,*args,**kwargs):
print "calling some_property setter({0},{1},{2})".format(self,args,kwargs)
self._some_property = args[0]
@property
def some_other_property(self,*args,**kwargs):
print "calling some_other_property getter({0},{1},{2})".format(self,args,kwargs)
return self._some_other_property
o = MyClass()
# 未装饰的方法还是正常的行为方式,需要当前的类实例(self)作为第一个参数。
o.normal_method
# <bound method MyClass.normal_method of <__main__.MyClass instance at 0x7fdd2537ea28>>
o.normal_method()
# normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>,),{})
o.normal_method(1,2,x=3,y=4)
# normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>, 1, 2),{'y': 4, 'x': 3})
# 类方法的第一个参数永远是该类
o.class_method
# <bound method classobj.class_method of <class __main__.MyClass at 0x7fdd2536a390>>
o.class_method()
# class_method((<class __main__.MyClass at 0x7fdd2536a390>,),{})
o.class_method(1,2,x=3,y=4)
# class_method((<class __main__.MyClass at 0x7fdd2536a390>, 1, 2),{'y': 4, 'x': 3})
# 静态方法(static method)中除了你调用时传入的参数以外,没有其他的参数。
o.static_method
# <function static_method at 0x7fdd25375848>
o.static_method()
# static_method((),{})
o.static_method(1,2,x=3,y=4)
# static_method((1, 2),{'y': 4, 'x': 3})
# @property是实现getter和setter方法的一种方式。直接调用它们是错误的。
# “只读”属性可以通过只定义getter方法,不定义setter方法实现。
o.some_property
# 调用some_property的getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
# 'properties are nice'
# “属性”是很好的功能
o.some_property()
# calling some_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: 'str' object is not callable
o.some_other_property
# calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
# 'VERY nice'
# o.some_other_property()
# calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{})
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: 'str' object is not callable
o.some_property = "pythontab"
# calling some_property setter(<__main__.MyClass object at 0x7fb2b7077890>,('pythontab',),{})
o.some_property
# calling some_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
# 'pythontab'
o.some_other_property = "pythontab.com"
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# AttributeError: can't set attribute
o.some_other_property
# calling some_other_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
最新文章
- 移动端IOS点击事件失效解决方案
- C#操作内存读写方法的主要实现代码
- redis服务和扩展安装(windows)
- 解决Visual Studio 2010/2012在调试时lock文件的方法
- 用Qt开发第一个Hello World程序
- DP:Apple Catching(POJ 2385)
- 使用mysql profiling功能剖析单条查询
- ASP.NET全局文件与防盗链
- iOS_SN_详解沙河(转载)
- JavaScript权威指南科13章 webj浏览器avascript
- ubuntu-16.04(linux)使用Reaver爆破wifi密码(路由器的WPS功能漏洞)
- Java设计模式:桥接模式
- 阿里巴巴矢量图标库(Iconfont)-利于UI和前端的搭配
- sqlldr的用法 (这个最完整)
- Guava Preconditions 工具参数前置校验
- python学习之旅(四)
- typedef 用法总结
- PHP测试Mysql数据库连接
- 基于.NET Standard的分布式自增ID算法--Snowflake代码实现
- 【LOJ#10064】黑暗城堡
热门文章
- POJ3668 Game of Lines
- java IO的字节流和字符流及其区别
- Application binary interface and method of interfacing binary application program to digital computer
- nginx 根据url访问次数限制
- array数据初始化
- javascript原型理解一种
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
- 手机估值计算的jquery代码
- LCA【p4281】[AHOI2008]紧急集合 / 聚会
- Editing 2011-2012 ACM-ICPC Northeastern European Regional Contest (NEERC 11)