‘self’ 是什么意思? 在@classmethod 中引用?

发布于 2024-07-13 15:00:23 字数 189 浏览 7 评论 0原文

我以为我开始掌握“Python 方式”编程了。 类的方法接受 self 作为第一个参数,以引用该方法在其上下文中调用的类的实例。 @classmethod 装饰器引用其功能与该类相关联的方法,但不引用具体实例。

那么,如果要在没有实例引用的情况下调用该方法,那么 @classmethod 的第一个参数(规范上的“self”)指的是什么呢?

I thought I was starting to get a grip on "the Python way" of programming. Methods of a class accept self as the first parameter to refer to the instance of the class whose context the method is being called in. The @classmethod decorator refers to a method whose functionality is associated with the class, but which doesn't reference a specific instance.

So, what does the first parameter of a @classmethod (canonically 'self') refer to if the method is meant to be called without an instance reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偏闹i 2024-07-20 15:00:23

类本身

类方法接收类作为隐式第一个参数,就像实例方法接收实例一样。

class C:
    @classmethod
    def f(cls):
        print(cls.__name__, type(cls))

>>> C.f()
C <class 'type'>

顺便说一句,它是规范的 cls

class itself:

A class method receives the class as implicit first argument, just like an instance method receives the instance.

class C:
    @classmethod
    def f(cls):
        print(cls.__name__, type(cls))

>>> C.f()
C <class 'type'>

and it's cls canonically, btw

七婞 2024-07-20 15:00:23

按照惯例,类方法的第一个参数被命名为 cls,并引用调用该方法的类对象。

>>> class A(object):
...     @classmethod
...     def m(cls):
...         print cls is A
...         print issubclass(cls, A)

>>> class B(A): pass
>>> a = A()
>>> a.m()
True
True
>>> b = B()
>>> b.m()
False 
True

The first parameter of a classmethod is named cls by convention and refers to the the class object on which the method it was invoked.

>>> class A(object):
...     @classmethod
...     def m(cls):
...         print cls is A
...         print issubclass(cls, A)

>>> class B(A): pass
>>> a = A()
>>> a.m()
True
True
>>> b = B()
>>> b.m()
False 
True
怪我闹别瞎闹 2024-07-20 15:00:23

类对象作为第一个参数传递。 例如:

class Foo(object):
    @classmethod
    def bar(self):
        return self()

将返回 Foo 类的实例。

编辑

请注意,最后一行是 self() 而不是 self。 self 将返回类本身,而 self() 返回一个实例。

The class object gets passed as the first parameter. For example:

class Foo(object):
    @classmethod
    def bar(self):
        return self()

Would return an instance of the Foo class.

EDIT:

Note that the last line would be self() not self. self would return the class itself, while self() returns an instance.

清风无影 2024-07-20 15:00:23

Django 在这里用类方法做了一些奇怪的事情:

class BaseFormSet(StrAndUnicode):
    """
    A collection of instances of the same Form class.
    """
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                 initial=None, error_class=ErrorList):
        ...
        self.prefix = prefix or self.get_default_prefix()
        ...

即使 get_default_prefix 是这样声明的(在同一个类中):

    @classmethod
    def get_default_prefix(cls):
        return 'form'

Django does some strange stuff with a class method here:

class BaseFormSet(StrAndUnicode):
    """
    A collection of instances of the same Form class.
    """
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                 initial=None, error_class=ErrorList):
        ...
        self.prefix = prefix or self.get_default_prefix()
        ...

Even though get_default_prefix is declared this way (in the same class):

    @classmethod
    def get_default_prefix(cls):
        return 'form'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文