什么是班级方法? [英] What is class method?

查看:77
本文介绍了什么是班级方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我熟悉静态方法概念,但是什么是类方法?

它与静态方法有什么不同?何时使用?

-

等级M:

def方法(cls,x):

通过


方法= classmethod(方法)

-

谢谢你的时间。

Hi,
I''m familiar with static method concept, but what is the class method?
how it does differ from static method? when to use it?
--
class M:
def method(cls, x):
pass

method = classmethod(method)
--
Thank you for your time.

推荐答案

8月24日,6:32 * pm,Hussein B< hubaghd ... @ gmail.comwrote:
On Aug 24, 6:32*pm, Hussein B <hubaghd...@gmail.comwrote:



我熟悉静态方法概念,但是什么是类方法?

它与静态方法有什么不同?何时使用?

-

等级M:

* def方法(cls,x):

* *通过


*方法= classmethod(方法)

-

感谢您的时间。
Hi,
I''m familiar with static method concept, but what is the class method?
how it does differ from static method? when to use it?
--
class M:
*def method(cls, x):
* * pass

*method = classmethod(method)
--
Thank you for your time.



首先,不要使用method = classmethod(方法)。装饰者远远超过
更好。以下代码具有相同的效果:


类M:

@classmethod

def方法(cls,x):

通过


更具可读性,对吧?

如果你有很多继承发生。

传入的第一个参数是调用方法的类。 Handy

for mix-in:它可以添加影响实际类的方法,它混合了b
,而不是混淆混合本身。 />

Firstly, don''t use method = classmethod(method). Decorators are far
better. The following code has the same effect:

class M:
@classmethod
def method(cls, x):
pass

Far more readable, right?

Class methods are useful if you''ve got lots of inheritance happening.
The first argument passed in is the class calling the method. Handy
for a mix-in: it can add methods affecting the actual class it''s mixed
into, rather than messing with the mix-in itself.


Hussein B< hu ******** @ gmail.comwrites:
Hussein B <hu********@gmail.comwrites:



我熟悉静态方法概念,但是什么是类方法?

它与静态方法有什么不同?何时使用?

-

等级M:

def方法(cls,x):

传递


方法= classmethod(方法)
Hi,
I''m familiar with static method concept, but what is the class method?
how it does differ from static method? when to use it?
--
class M:
def method(cls, x):
pass

method = classmethod(method)



当你的方法需要知道调用它的类时使用它。

这在子类化的上下文中是有意义的:


类M(对象):

@classmethod

def方法(cls,x):

print cls,x

class N(M):

pass

Use it when your method needs to know what class it is called from.
This makes sense in the context of subclassing:

class M(object):
@classmethod
def method(cls, x):
print cls, x

class N(M):
pass


>> M.method(1)
>>M.method(1)



< class''__ main __。M''1

<class ''__main__.M''1


>> N.method(1)
>>N.method(1)



< class''__ main__。 N''1

<class ''__main__.N''1


8月24日凌晨3点35分,MeTheGameMakingGuy< go psychona ... @ gmail.com>

写道:
On Aug 24, 3:35*am, MeTheGameMakingGuy <gopsychona...@gmail.com>
wrote:

8月24日,6:32 * pm,Hussein B< hubaghd ... @ gmail.comwrote:
On Aug 24, 6:32*pm, Hussein B <hubaghd...@gmail.comwrote:



我熟悉静态方法概念,但是什么是类方法?

它与静态方法有什么不同?何时使用?

-

等级M:

* def方法(cls,x):

* *传递
Hi,
I''m familiar with static method concept, but what is the class method?
how it does differ from static method? when to use it?
--
class M:
*def method(cls, x):
* * pass


* method = classmethod(method)

-

谢谢你的时间。
*method = classmethod(method)
--
Thank you for your time.



首先,不要使用method = classmethod(method)。装饰者远远超过
更好。以下代码具有相同的效果:


类M:

* @ classmethod

* def方法(cls,x) :

*通过


更具可读性,对吧?

类方法非常有用发生了大量的继承。

传入的第一个参数是调用方法的类。 Handy

for mix-in:它可以添加影响实际类的方法,它混合了b
,而不是混淆混合本身。


Firstly, don''t use method = classmethod(method). Decorators are far
better. The following code has the same effect:

class M:
*@classmethod
*def method(cls, x):
* pass

Far more readable, right?

Class methods are useful if you''ve got lots of inheritance happening.
The first argument passed in is the class calling the method. Handy
for a mix-in: it can add methods affecting the actual class it''s mixed
into, rather than messing with the mix-in itself.



类似于:


等级M:#not correct如图所示

def newmaker(self,x):

newinst = self .__ class __(arg1,arg2,x)

返回newinst


m1 = M()

m2 = m1.newmaker(''abc'')


除了你不需要第一个实例来做。注意你

得到m1是实例的新实例,而不是

必然M.


class N (M):

通过


m1 = N()

m2 = m1.newmaker(''abc'')


m2现在也是N级。

It is similar to:

class M: #not correct as shown
def newmaker( self, x ):
newinst= self.__class__( arg1, arg2, x )
return newinst

m1= M()
m2= m1.newmaker( ''abc'' )

except you don''t need the first instance to do it with. Notice you
get a new instance of whatever class m1 is an instance of, rather than
necessarily M.

class N( M ):
pass

m1= N()
m2= m1.newmaker( ''abc'' )

m2 is of class N now too.


这篇关于什么是班级方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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