简体   繁体   English

在python中调用classmethod内部的方法

[英]call a method inside classmethod in python

I am learning python and stucked in a problem我正在学习 python 并遇到了问题

Suppose I have a class:假设我有一个类:

class Xyz:
    def __init__(self):
        self.number=25
    
    def square(self):
        return self.number*self.number
    
    @classmethod
    def getsquare(cls):
        return cls.square()

#Now let's call getsquare() method
sq=Xyz.getsquare()

I am getting an error:我收到一个错误:

TypeError: square() missing 1 required positional argument: 'self'

My attempt:我的尝试:

I tried to make square() function as classmethod and then calling the getsquare() method but still I am getting an error (I guess it was because since we aren't creating an object of class so due to this number is not initialising)我试图将 square() 函数设为classmethod然后调用 getsquare() 方法,但仍然出现错误(我猜这是因为我们没有创建类的对象,因此由于这个数字没有初始化)

but if I do like this it's working:但如果我喜欢这个它的工作原理:

class Xyz:
    
    def square():
        number=25
        return number*number
    
    @classmethod
    def getsquare(cls):
        return cls.square()

so How to call a class function inside classmethod?那么如何在类方法中调用类函数呢?

any help or clue will be appriciated任何帮助或线索将被appriciated

Some mistakes I see here, but I am going to answer the syntax question我在这里看到了一些错误,但我将回答语法问题

@classmethod / @staticmethod decorate a method as a static member for the class. @classmethod / @staticmethod将方法装饰为类的静态成员。 By definition, they do not reference to any object and have several restrictions:根据定义,它们不引用任何对象并且有几个限制:

  • They can only directly call other static method它们只能直接调用其他静态方法
  • They can only directly access static data他们只能直接访问静态数据
  • They cannot refer to self or super in any way他们不能以任何方式提及 self 或 super

In your code, getsquare is a static method and square is a instance method.在您的代码中, getsquare是一个静态方法,而square是一个实例方法。 So, getsquare violates the first rule because of it is calling to square所以, getsquare违反了第一条规则,因为它调用了square

cls is just an uninstantiated class object, calling return cls.square() is analogous to calling Xyz.square() , which obviously doesn't work because its not a @classmethod . cls只是一个未实例化的类对象,调用return cls.square()类似于调用Xyz.square() ,这显然不起作用,因为它不是@classmethod Try first initiating the class inside getsquare before calling any methods:在调用任何方法之前,先尝试在getsquare初始化类:

class Xyz:
    def __init__(self):
        self.number=25
    
    def square(self):
        return self.number*self.number
    
    @classmethod
    def getsquare(cls):
        return cls().square()

#Now let's call getsquare() method
sq=Xyz.getsquare()

Why the second example works, the simple answer is because it doesn't have an __init__ defined so it's doesn't know how to initialize so it's not needed.为什么第二个例子有效,简单的答案是因为它没有定义__init__所以它不知道如何初始化所以不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python/Tornado - 调用类方法 - Python/Tornado - call to classmethod 在Python 3中,不能在元类的方法内调用“ classmethod”对象 - 'classmethod' object is not callable inside a metaclass's method in Python 3 Python __call __()这是一个隐式的类方法吗? - Python __call__() is this an implicit classmethod? python用实例方法覆盖classmethod - python overriding a classmethod with an instance method 如何从Python中重写的@classmethod调用父类的@classmethod? - How to call a parent class's @classmethod from an overridden @classmethod in Python? 装饰已经用@classmethod装饰的Python方法,该方法调用另一个@classmethod - Decorating a Python method already decorated with @classmethod which calls another @classmethod python子类方法调用用非类方法覆盖的类方法 - python child class method calling overridden classmethod with non-classmethod 我们可以在@classmethod function 中调用用户定义的实例方法吗? - Can we call a user-defined instance method inside a @classmethod function? 从实例中调用类方法作为方法是不好的形式吗? - Is it bad form to call a classmethod as a method from an instance? 如何修补 python @classmethod 以调用我的 side_effect 方法? - How do I patch a python @classmethod to call my side_effect method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM