使staticmethod对象可调用吗? [英] Make staticmethod objects callable?
问题描述
大家好,
我想知道制作静态方法对象是否有意义
可调用,以便以下代码可以正常工作:
A类:
@staticmethod
def foo():传递
bar = foo()
我理解staticmethod对象不需要为其他用例实现__call__
,但实现它仍然有意义
__call__用于该特定用例?它会以任何方式容易出错吗?
Thx和问候,
Nicolas
Hi everyone,
I was wondering if it would make sense to make staticmethod objects
callable, so that the following code would work:
class A:
@staticmethod
def foo(): pass
bar = foo()
I understand staticmethod objects don''t need to implement __call__ for
their other use cases, but would it still make sense to implement
__call__ for that specific use case? Would it be error-prone in any way?
Thx and regards,
Nicolas
推荐答案
Nicolas Fleury写道:
Nicolas Fleury wrote:
我想知道使静态方法对象可以调用是否有意义,以便下面的代码可以工作:
A级:
@staticmethod
def foo():传递
bar = foo()
I was wondering if it would make sense to make staticmethod objects
callable, so that the following code would work:
class A:
@staticmethod
def foo(): pass
bar = foo()
你有吗?现实世界的用例?我几乎从不使用
staticmethods(宁愿选择使用模块级函数)所以我很难说b $ b很难想出一些代码真的
很有用。我也有点想要打破
classmethod和staticmethod之间的平行关系,这些方法目前只是*描述符
(没有任何其他特殊功能)。
也许而不是只使staticmethods可以调用,静态方法
和classmethods可以获得一个''im_func''属性,比如绑定和
unbound方法有?
STeVe
Do you have a real-world use case? I pretty much never use
staticmethods (preferring instead to use module-level functions) so I''m
having a hard time coming up with some code where this would be really
useful. I''m also a little wary of breaking the parallel between
classmethod and staticmethod which are currently *just* descriptors
(without any other special functionality).
Maybe instead of making just staticmethods callable, both staticmethods
and classmethods could gain an ''im_func'' attribute like bound and
unbound methods have?
STeVe
Em Ter,2006-02-28?* s 15:17 -0500,Nicolas Fleury escreveu:
Em Ter, 2006-02-28 ?*s 15:17 -0500, Nicolas Fleury escreveu:
A类:
@staticmethod
def foo():传递
bar = foo()
class A:
@staticmethod
def foo(): pass
bar = foo()
#为什么不:
def foo():通过
A级:
吧= foo()
foo = staticmethod(foo)
-
Quem excele em empregar a for?§一个militar subjulga os ex ?? rcitos dos
outros povos sem travar batalha,toma cidades fortificadas dos outros
povos sem as atacar e destr?3i os estados dos outros povos sem lutas
prolongadas。 Deve lutar sob o C ?? u com o prop?3sito primordial da
''preserva?§?£o''。 Desse modo suas armas n?£se se embotar?£o,e os ganhos
poder?£o ser preservados。 Essa ?? a estrat ?? gia para planejar ofensivas。
- Sun Tzu,emA arte da guerra
# Why not:
def foo(): pass
class A:
bar = foo()
foo = staticmethod(foo)
--
"Quem excele em empregar a for?§a militar subjulga os ex??rcitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destr?3i os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o C??u com o prop?3sito primordial da
''preserva?§?£o''. Desse modo suas armas n?£o se embotar?£o, e os ganhos
poder?£o ser preservados. Essa ?? a estrat??gia para planejar ofensivas."
-- Sun Tzu, em "A arte da guerra"
Felipe Almeida Lessa写道:
Felipe Almeida Lessa wrote:
Em Ter,2006-02-28?* s 15:17 -0500,Nicolas Fleury escreveu:
Em Ter, 2006-02-28 ?*s 15:17 -0500, Nicolas Fleury escreveu:
A级:
@staticmethod
def foo():传递
bar = foo()
class A:
@staticmethod
def foo(): pass
bar = foo()
#为什么不:
def foo():传递
A类:
bar = foo()
foo = staticmethod(foo)
# Why not:
def foo(): pass
class A:
bar = foo()
foo = staticmethod(foo)
>
好吧,你甚至可以这样做:
A级:
def foo():传递
bar = foo()
staticmethod(foo)
但是,为装饰器设置特殊语法会有点难过。这是一个
可用性问题,与功能无关。有明显的
解决方法,但恕我直言,任何用户说它应该只是工作部分正确至少
。
问候,
Nicolas
Well, you could even do:
class A:
def foo(): pass
bar = foo()
staticmethod(foo)
But it''s a bit sad to have a special syntax for decorators then. It''s a
usability problem, nothing to do with functionality. There''s obvious
workarounds, but IMHO any user saying "it should just work" is at least
partly right.
Regards,
Nicolas
这篇关于使staticmethod对象可调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!