问题描述
python'的staticmethod相当于java staticmethod吧?
with classmethod,我可以调用方法一个实例不需要创建
吧?因为两者之间的差异是
classmethod将类本身作为implicti第一个参数接收。来自
我的理解类方法是用于处理类属性吗?
有人能教我实际使用classmethod& staticmethod?
谢谢
james
python''s staticmethod is the equivalent of java staticmethod right?
with classmethod, I can call the method without the need for creating
an instance right? since the difference between the two is that
classmethod receives the class itself as implicti first argument. From
my understanding classmethod are for dealing with class attributes?
Can somebody teach me the real use of classmethod & staticmethod?
Thanks
james
推荐答案
正确。 `staticmethod`本质上只是一个移入类
的函数,可以在类对象和该类的实例中访问。
因为Python反对Java函数,`staticmethod`不是那么有用。
Correct. `staticmethod` is essentially just a function moved into a class
and accessible at the class object and instances of that class.
As Python opposed to Java has functions, `staticmethod` isn''t that useful.
可以访问类属性,也许更重要的是,它可以调用类来返回一个实例。因此,如果在子类上调用
`classmethod`,则返回该子类的实例。
傻例子:
A类(对象):
def __init __(self,x,y):
print''init A''
self.x = x
self.y = y
@classmethod
def from_str(cls,string):
返回cls(* map(float,string.split('','')))
class B(A):
def __init __(self, x,y):
打印''初始B''
A .__ init __(self,x,y)
def main() :
B.from_str('''42,23'')
Ciao,
Marc''BlackJack''Rintsch
It''s possible to access class attributes and, maybe more important, it''s
possible to call the class to return an instance. So if you call a
`classmethod` on a subclass an instance of that subclass is returned.
Silly example:
class A(object):
def __init__(self, x, y):
print ''init A''
self.x = x
self.y = y
@classmethod
def from_str(cls, string):
return cls(*map(float, string.split('','')))
class B(A):
def __init__(self, x, y):
print ''init B''
A.__init__(self, x, y)
def main():
B.from_str(''42,23'')
Ciao,
Marc ''BlackJack'' Rintsch
IIRC,是的。 staticmethod实际上只不过是一个附加到类的函数
,可以在类或实例上调用
。请注意,由于Python支持模块和函数,staticmethods
几乎没有实际用途。
IIRC, yes. A ''staticmethod'' is in fact nothing more than a function
attached to a class, and which can be called on the class or an instance
of. Note that since Python supports modules and functions, staticmethods
are of little practical use.
是。
Yes.
不一定 - 您可以从
实例方法中访问类属性(但显然类方法无法访问实例
属性)。
Not necessarily - you can access class attributes from within an
instance method (but obviously a classmethod cannot access instance
attributes).
''真实''用途是你会找到的那个。 FWIW,我使用
staticmethods来获取不需要访问类
或实例的辅助函数,但是对于类来说太具体了,不能用作普通类
功能。这不是一个非常常见的情况。 Classmethods更多
有用 - 主要作为
替代构造函数的替代构造函数或实用程序方法,但还有其他可能的用法(抱歉,我有
手头没有具体的例子。
The ''real'' use is (are) the one(s) you''ll find. FWIW, I use
staticmethods for helper functions that don''t need access to the class
or instance but are too specific to a class to be of any use as plain
functions. Which is not a very frequent case. Classmethods are more
usefull - mostly as alternate constructors or utility methods for an
alternate constructor, but there are other possible uses (sorry, I have
no concrete example at hand).
你的意思是像Marc的例子
谢谢
james
You mean like the example from Marc
Thanks
james
这篇关于classmethod&静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!