4

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?

Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
legameeternoforall
  • 69
  • 1
  • 2
  • 10
  • While writing last sentence `Can I even call car_method as a class method? or it is just a method defined inside class?`, I realised may be it is behaving differently, and after experimenting further, I realised, it is indeed a class method that can be used inside the class but not like `self.car_method()`. This will still give error. But calling it just like `car_method()` will work just fine provided you dont use it in any other class method!!!! it can be used as an class attribute!! – legameeternoforall Dec 03 '17 at 07:47

5 Answers5

7

In Python, all the methods of the class receive an instance of the class object implicitly as the first argument.

It's general practice in Python to name the first argument in class methods as self it's like this in C and Java-like languages. The only difference, it's not a reserved keyword and you can name it anything. Though it's a general convention to use self as it is more readable to Python programmers and Python documentation, IDE parsers. Read the docs.

You can access class methods and variables across methods of the class using self. A very simple example showing the use case of self:

In [37]: class car:
    ...:     def __init__(self):
    ...:         self.car_name = 'Jaguar'
    ...:
    ...:     def car_method(self):
    ...:         print("I can access " + self.car_name + " because I have
    ...: self")
    ...:         print("Inside Car method")
    ...:
    ...:     def another_method(self):
    ...:         # this calls car_method
    ...:         self.car_method()
    ...:

In [38]: c = car()

In [39]: c.another_method()
I can access Jaguar because I have self
Inside Car method

If you want a classmethod. You can define a class method in Python as:

class car:

    @classmethod
    def car_method(cls):
        print("Inside Car method")

The little thing @classmethod on top of the car_method method is called decorator in Python. But even in class methods, the class instance is passed as an argument in car_method.

If you want to call your method car_method without an argument and do not need to access class/instance variables(these are called static methods). You can define your method as a static method with a @staticmethod decorator.

class car:

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

Here no self is passed as argument and you can call car_method as car().car_method()

Why Python allow car_method to be created even when self is passed as an argument?

It's because Python is not statically typed like C. It's a dynamic language. Unlike statically-typed languages, all the type, function argument, etc checking happens at runtime in Python

To know more about @classmethod and @staticmethod decorator, read about @classmethod and @staticmethod in this awesome SO thread

Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
  • thanks for responding, whay you mentioned are `classmethod` and `staticmethod`, but I wanted to know what is a method inside a class that is not either of them? how does it behave, its benifits/uses and how do I call it? I added a comment after I posted the post, so please comment on that as well. I am curious to know this! – legameeternoforall Dec 03 '17 at 07:58
  • @legameeternoforall Added an example in the answer. I suggest you read the doc I attached. It gives a very good start about OOP in Python. – Amit Tripathi Dec 03 '17 at 08:10
  • @legameeternoforall Feel free to [accept my answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you feel it was useful to you. :-) – Amit Tripathi Dec 03 '17 at 09:34
1

in python a method of class is defined by like this:

class MyObj(object):

    def my_method(self):
        pass

always at least one first argument which is the instance itself.

Yun Luo
  • 1,506
  • 1
  • 9
  • 10
0

To access ca_method() inside car class, you just need to create an instance of the car class and then use the method. For example:

class car:

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

a = car()
a.car_method()

or just simply:

class car:
    
    def car_method():
           print("Inside Car method")
    
car().car_method()
Nitish
  • 392
  • 2
  • 7
0

TESTED Add "self" argument.

class car:
      def car_method(self):
         print("Inside Car Method") 
obj=car() 
obj.car_method()

I had the same challenge but adding the self resolved it.

0

self is a instance of class ion python. you will get error if you avoid self as argument. if you don't have any arguments to pass to the method, you have three ways to overcome.

1 set self as None, if you don't have any arguments to pass

class car:
def car_method(self=None):
    print("test text")
obj = car
obj.car_method()

2 You can define your method as a static method with a @staticmethod decorator

class car:
@staticmethod
def car_method():
    print("Inside Car method")

obj = car
obj.car_method()

3 pass the instance of the object by putting a bracket at the end of object call

class car:
def car_method(self):
    print("test text")

obj = car()
obj.car_method()
Rashid
  • 1
  • 1