类方法与任何其他可调用方法 [英] Class Methods Vs Any Other Callable

查看:73
本文介绍了类方法与任何其他可调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得在Python中学习闭包,并认为这是最愚蠢的想法。为什么在Python完全面向对象时使用闭包?在我开始学习之前我没有掌握它们的力量/原因

JavaScript然后是BAM,我理解它们。


就在不久之前,我害怕装饰者因为我真的没有找到明确的资源来学习它们(如何使用@)。

他们有多重要?它们一定很重要,否则为什么用这种语言会'b $

?我不得不学习'',然后突然,BAM。我理解了他们。


关于闭包和装饰的主要问题隐藏在

如何*死简单*他们是。我需要的只是使用它们的原因

而不是X风格。那么我的观点是什么?怎么死简单的类

方法?我一定要错过这一点,所以我确信它们必须是简单的




类,函数,实例和静态方法都很简单。这么容易实际上,我可以在不看的情况下用脚射击自己(最好是没有瞄准的
)。那么,为什么我坚持使用类方法的* idea *?

实例方法适用于实例

静态方法基本上是一个函数嵌套在类对象中

类方法是否过度?


我可以通过类或任何
$调用静态或类方法b $ b的实例。我从来没有设计过一种利用

类名的方法,除非我需要扩展超类

*但是*即使在这种情况下,我没有使用封闭的班级名称......


什么是类方法的交易,为什么要使用它们呢?

什么是class方法在至少一行中完成比

还要其他什么?它有助于减少重复或打字吗?我因为至少*一个*使用它们的好理由而丢失了




使用它们的最大理由是什么? ?一点点语法和

解释可以走很长的路。我没有理解它们所以

这里真的很感激任何帮助!

I remember learning closures in Python and thought it was the dumbest
idea ever. Why use a closure when Python is fully object oriented? I
didn''t grasp the power/reason for them until I started learning
JavaScript and then BAM, I understood them.

Just a little while ago, I had a fear of decorators because I really
couldn''t find a definitive source to learn them (how to with with @).
How important are they? They must be important otherwise why have''em
in the language? I had to learn''em and then suddenly, BAM. I
understand them.

My main issue with closures and decorators was hidden in the fact of
how *dead simple* they were. All I needed were reasons to use them
over doing it X style. So what is my point? How dead simple are class
methods? I must be missing there point so I am convinced they must be
dead simple.

classes, functions, instance and static methods are easy. So easy in
fact, I could shoot myself in the foots without looking (preferably
without aiming). So, why am I stuck on the *idea* of a class method?

An instance method works on the instance
A Static method is basically a function nested within a class object
A class method is overkill?

I can call a static or class method through either the class OR any
instance of it. I''ve never designed a method that took advantage of
the class name except in cases where I needed to extend a super class
*but* even in this case, I didn''t use the enclosing class name...

Whats the deal with class methods, why use them over anything else?
What does a class method accomplish in at least one line shorter than
anything else? Does it help reduce duplication or typing? I am at a
lost for words that can shed at least *one* good reason to use them.

What is the one greatest reason to use them? A little syntax and
explanation can go a long long way. I am failing to understand them so
any help is really appreciated here!

推荐答案

实例方法适用于实例
An instance method works on the instance

静态方法基本上是嵌套在类对象中的函数

类方法是否过度?
A Static method is basically a function nested within a class object
A class method is overkill?



如果有的话,静态方法是过度的。通过这种方式看待:*如果*你为某些

的原因将一个方法放入一个封闭的上下文中 - 不值得对它进行

的引用吗? />

If anything, a static method is overkill. See it this way: *if* you for some
reason put a method into an enclosing context - isn''t it worth having a
reference to that?


我可以通过类或任何

实例调用静态或类方法。我从来没有设计过一种利用

类名的方法,除非我需要扩展超类

*但是*即使在这种情况下,我没有使用封闭的班级名称......


什么是类方法的交易,为什么要使用它们呢?

什么是class方法在至少一行中完成比

还要其他什么?它有助于减少重复或打字吗?我因为至少*一个*使用它们的好理由而丢失了




使用它们的最大理由是什么? ?一点点语法和

解释可以走很长的路。我没有理解他们所以

任何帮助都非常感谢!
I can call a static or class method through either the class OR any
instance of it. I''ve never designed a method that took advantage of
the class name except in cases where I needed to extend a super class
*but* even in this case, I didn''t use the enclosing class name...

Whats the deal with class methods, why use them over anything else?
What does a class method accomplish in at least one line shorter than
anything else? Does it help reduce duplication or typing? I am at a
lost for words that can shed at least *one* good reason to use them.

What is the one greatest reason to use them? A little syntax and
explanation can go a long long way. I am failing to understand them so
any help is really appreciated here!



没有一个最好的理由。对于类的实例,它们有用作例如
工厂方法。或者例如注册

函数用于在类的实例中进行通知。在这种情况下

写作


class Foo:


@classmethod

def register(cls,listener):

cls.LISTENERS.append(听众)


更强大,因为你可以按你喜欢的方式重命名Foo - 注册

不会受到影响。


Diez


There is not one greatest reason. They have their uses as e.g.
factory-methods for instances of the class. Or as e.g. registering
functions for notifications amongst instances of the class. In which case
writing

class Foo:

@classmethod
def register(cls, listener):
cls.LISTENERS.append(listener)

is much more robust because you could rename Foo the way you like - register
won''t be affected.

Diez



5月14日上午10点19分,Diez B. Roggisch < de ... @nospam.web.dewrote:
On May 14, 10:19 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:

实例方法适用于实例

静态方法基本上是嵌套在类对象中的函数

类方法是否过度?
An instance method works on the instance
A Static method is basically a function nested within a class object
A class method is overkill?



如果有的话,静态方法是过度的。通过这种方式看待它:*如果*你为某些

的原因将一个方法放入一个封闭的上下文中 - 是不是值得对它进行

的引用?


If anything, a static method is overkill. See it this way: *if* you for some
reason put a method into an enclosing context - isn''t it worth having a
reference to that?



我的确切感觉;这些天我几乎总是使用类方法

而不是静态。我依稀记得在某处看到一个例子

,其中静态方法是唯一(优雅)的解决方案;既不是一个类

方法也不是普通函数。如果我发现它,我会发布它除非

有人打败我。


George

My feeling exactly; these days I almost always use class methods
instead of static. I vaguely remember seeing somewhere an example
where a static method was the only (elegant) solution; neither a class
method nor a plain function would do. I''ll post it if I find it unless
someone beats me to it.

George


实例方法适用于实例
An instance method works on the instance

静态方法基本上是嵌套在类对象中的函数

类方法是否过度?
A Static method is basically a function nested within a class object
A class method is overkill?



如果有的话,静态方法是过度的...

class Foo:


* * @ classmethod

* * def register(cls,listener):

* * * * cls.LISTENERS.append(listener)


If anything, a static method is overkill...
class Foo:

* *@classmethod
* *def register(cls, listener):
* * * *cls.LISTENERS.append(listener)



当我学习静态方法时,我了解到它们是一种方式来确保将某些功能与一个类紧密地结合在一起而无需绑定

任何实例的功能。我认为它们只不过是一个设计决定。对我来说,他们有一定意义。


除了方法签名(classmethod(cls,l)和

staticmethod(l))之外,类方法可以什么静态方法

做什么并免费获得CLS参考?这是为什么静态方法

被认为是矫枉过正?换句话说,任何一个都可以从类或实例中被称为

,并且两者都可以相同地工作,但是*只有类方法包括类参考和

静态方法不是吗?

我在一个实例和一个类之间看到的唯一真正的区别

或static方法是整个绑定/未绑定的东西。否则,即使是一个

实例也可以做其他人做的事情*只是*实例方法只能通过实例而不是类来进行调用。


实例方法最有意义。一个静态的方法也很有意义

*但是*我可以看到一个类方法不仅如何做静态方法

的作用,而且类方法*也如何得到cls免费参考。


我是否正确?

When I learned about static methods, I learned they''re a way to
tightly couple some functionality with a class without tying the
functionality to any of the instances. I see them as nothing more than
a design decision. To me they make some sense.

Other than a methods signature (classmethod(cls, l) and a
staticmethod(l)) a class method does anything that a static method
does and gets the CLS reference for FREE? Is this why a static method
is considered to be overkill? In other words, either one can be called
from either the class or the instance and both work pretty much the
same *but* only the class method includes the class for reference and
the static method does not?

The only real difference I see between an instance and either a class
or static method is the whole bound/unbound thing. Otherwise, even an
instance can do what the others do *just* the instance method can only
make those calls through an instance and not the class.

Instance methods make the most sense. A static method makes sense too
*but* I can see how a class method not only does what a static method
does but how a class method *also* gets the cls reference for free.

Am I correct?


这篇关于类方法与任何其他可调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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