栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在类主体中调用类staticmethod?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在类主体中调用类staticmethod?

staticmethod
对象显然具有
__func__
存储原始原始函数的属性(它们必须这样做)。所以这将工作:

class Klass(object):    @staticmethod  # use as decorator    def stat_func():        return 42    _ANS = stat_func.__func__()  # call the staticmethod    def method(self):        ret = Klass.stat_func()        return ret

顺便说一句,尽管我怀疑静态方法对象具有某种存储原始功能的属性,但我对具体细节一无所知。本着教别人钓鱼而不是给他们钓鱼的精神,这就是我所做的调查,并发现了这一点(Python会话中的C&P):

>>> class Foo(object):...     @staticmethod...     def foo():...         return 3...     global z...     z = foo>>> z<staticmethod object at 0x0000000002E40558>>>> Foo.foo<function foo at 0x0000000002E3CBA8>>>> dir(z)['__class__', '__delattr__', '__doc__', '__format__', '__func__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']>>> z.__func__<function foo at 0x0000000002E3CBA8>

在交互式会话中进行类似的挖掘(

dir
非常有帮助)通常可以非常快速地解决这些问题。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393646.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号