classmethod&静态方法 [英] classmethod & staticmethod

查看:63
本文介绍了classmethod&静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




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

推荐答案

On星期二,2007年7月24日03:19:05 +0000,james_027写道:
On Tue, 24 Jul 2007 03:19:05 +0000, james_027 wrote:

python'的staticmethod相当于java staticmethod吧?
python''s staticmethod is the equivalent of java staticmethod right?



正确。 `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.


with classmethod,我可以调用方法而不需要创建

an实例吧?因为两者之间的差异是

classmethod将类本身作为implicti第一个参数接收。从

我的理解classmethod是用于处理类属性?
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?



可以访问类属性,也许更重要的是,它可以调用类来返回一个实例。因此,如果在子类上调用

`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


james_027aécrit:
james_027 a écrit :

hi,


python 'static static方法相当于java staticmethod对吗?


python''s staticmethod is the equivalent of java staticmethod right?



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.


with classmethod,我可以调用方法而不用需要创建

一个实例吧?因为两者之间的差异是

classmethod将类本身作为implicti第一个参数接收。
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.



是。

Yes.


来自

我的理解类方法是用于处理类属性?
From
my understanding classmethod are for dealing with class attributes?



不一定 - 您可以从

实例方法中访问类属性(但显然类方法无法访问实例

属性)。

Not necessarily - you can access class attributes from within an
instance method (but obviously a classmethod cannot access instance
attributes).


有人可以教我对classmethod&的真正用法吗? STATICMETHOD?
Can somebody teach me the real use of classmethod & staticmethod?



''真实''用途是你会找到的那个。 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).


hi,


''真实''用法是(是)你会找到的那个人。 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&静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆