static class,下面這些是什么意思:@classmethod, @staticmethod, @property?

 2023-11-05 阅读 20 评论 0

摘要:@classmethod,?@staticmethod和@property這三個裝飾器的使用對象是在類中定義的函數。下面的例子展示了它們的用法和行為: class MyClass(object):def __init__(self): self._some_property = "properties are nice" self._some_other_proper

@classmethod,?@staticmethod@property這三個裝飾器的使用對象是在類中定義的函數。下面的例子展示了它們的用法和行為:

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,

轉載于:https://www.cnblogs.com/apollo1616/articles/9874055.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/166299.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息