我如何在类本身中调用python @staticmethodTypeError:“ staticmethod”对象不可调用?

问题描述

我有点费力去理解staticmethod在类中的表现方式,我创建了一些代码来阐明我的目的:

class klass(): 

    @staticmethod 
    def testA(x):
    
        return print(x+1)

    testA(x=1)

当我运行此代码时,它说:TypeError:“ staticmethod”对象不可调用 我的问题是:如何在同一个类中的类中调用函数

解决方法

首先,代码中的缩进是错误的。应该是:

class klass(): 

    @staticmethod 
    def testA(x):
    
        return print(x+1)

关于您的问题本身:调用静态方法需要类名称:

klass.testA(x=1)