【问题标题】:TypeError: str() missing 1 required positional argument: 'self'类型错误:str() 缺少 1 个必需的位置参数:'self'
【发布时间】:2016-05-27 00:22:15
【问题描述】:

我的代码有问题,我无法解决! 请原谅,我是新手..

我的代码:

class Case: 
'''
 '''

def __init__ (self):
    '''
    '''

    self.__valeur = 0
    self.__cache = True

def str(self):
    '''
    '''

    if self.__cache == True :
        return '-'
    if self.__valeur == -1 :
        return '*'
    if self.__valeur == 0 :
        return ' '
    else :
        return self.__valeur

错误:

    >>> demineur.Case.str()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: str() missing 1 required positional argument: 'self'

感谢您的帮助

【问题讨论】:

    标签: python class typeerror


    【解决方案1】:

    str()Case 类的实例方法。您有 2 个选项可以调用它:

    >>> instance = Case()
    >>> instance.str()
    

    >>> instance = Case()
    >>> Case.str(instance) 
    

    正如错误所说,您没有将Case 的实例传递给str 方法。

    【讨论】:

      【解决方案2】:

      首先,你不应该用内置的保留类型命名你的方法或变量,比如strlistint

      您可能正在尝试像调用静态方法一样调用该方法,创建类的实例并调用它

      Case.str() # Wrong because you're not passing an instance to it
      
      mycase = Case() 
      mycase.str() # my case automatically gets passed here implicity i.e mycase.str(mycase)
      

      【讨论】:

        猜你喜欢
        • 2022-01-03
        • 2021-07-14
        • 2015-07-13
        • 2019-10-02
        • 2017-06-24
        • 2017-08-21
        • 2021-09-21
        • 2020-11-10
        • 2018-04-05
        相关资源
        最近更新 更多