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
I think this error is because, In Python, every class method, when called is always passed at least one argument which is that object's reference. am I correct?
Then to experiment further, I wrote
obj.car_method <----- assuming I am not passing any argument to it
but the function printed nothing.
Then I tried to print the type of it using
print(obj.car_method)
which resulted in
bound method car.car_method of <__main__.car object at 0x005D2290>
Can anyone explain what is going on here? How do I 'call' the car_method? If there is no way to call the car_method, why does python let me define the method without any argument?
I am trying to get a good grasp on the basics here. Can I even call car_method as a class method? or it is just a method defined inside a class?