MrMartel
MrMartel

Reputation: 25

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

I have a problem with this code and I can't solve it ! Please excuse me, I'm a newbiee..

my code :

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

The error :

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

Thanks for your help

Upvotes: 1

Views: 1247

Answers (2)

danidee
danidee

Reputation: 9624

First of all you should not name your methods or variables after the in-built reserved types like str list and int

you're probably trying to call the method like a static method, create an instance of your class and call it

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)

Upvotes: 0

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

str() is an instance method of your Case class. You have 2 options to call it:

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

or

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

And as the error says, you are not passing an instance of Case to the str method.

Upvotes: 3

Related Questions