TypeError: str() missing 1 required positional argument: 'self'
TypeError: str() missing 1 required positional argument: 'self'
这段代码有问题,我无法解决!
请原谅,我是新手..
我的代码:
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'
感谢您的帮助
str()
是您的 Case
class 的实例方法。您有 2 个选项来调用它:
>>> instance = Case()
>>> instance.str()
或
>>> instance = Case()
>>> Case.str(instance)
正如错误所说,您没有将 Case
的实例传递给 str
方法。
首先,你不应该在内置的保留类型之后命名你的方法或变量,如 str
list
和 int
您可能正在尝试像调用静态方法一样调用方法,创建 class 的实例并调用它
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)
这段代码有问题,我无法解决! 请原谅,我是新手..
我的代码:
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'
感谢您的帮助
str()
是您的 Case
class 的实例方法。您有 2 个选项来调用它:
>>> instance = Case()
>>> instance.str()
或
>>> instance = Case()
>>> Case.str(instance)
正如错误所说,您没有将 Case
的实例传递给 str
方法。
首先,你不应该在内置的保留类型之后命名你的方法或变量,如 str
list
和 int
您可能正在尝试像调用静态方法一样调用方法,创建 class 的实例并调用它
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)