小编leg*_*all的帖子

Calling python class method which has no argument

I am new to python (coming from the c++ world) and was experimenting with class methods. I created a method without any argument (purposefully avoided self argument in this case). Then I tried to call it

class car:

    def car_method():
        print("Inside Car method")


obj = car_method()
obj.car_method()    <---- this creates error: TypeError: car_method() takes 0 positional arguments but 1 was given
Run Code Online (Sandbox Code Playgroud)

I think this error is because, In Python, every class method, when called is always passed at least one …

python self parameter-passing class-method python-3.x

4
推荐指数
1
解决办法
3342
查看次数