52

Suppose I have this code:

class Num:
    def __init__(self,num):
        self.n = num
    def getn(self):
        return self.n
    def getone():
        return 1
myObj = Num(3)

print(myObj.getn()) # result: 3

But if I try print(myObj.getone()), I get an error: 'getone()' takes no arguments (1 given).

So I replace:

def getone():
    return 1

with

def getone(self):
    return 1

Now print(myObj.getone()) shows 1, as expected. But - getone() doesn't need any arguments in order to just return 1. Do I have to use a meaningless argument?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Yugo Kamo
  • 2,349
  • 4
  • 18
  • 13
  • 8
    they are not meaningless. every method of a class always have one implicit argument, the instance. In C++ it's the same, but you never see it in the argument list because it magically appears out of the bushes of the fairy forest. Python is `Explicit is better than implicit`. – Stefano Borini Mar 02 '11 at 15:01
  • 2
    You can use @staticmethod and @classmethod decorators to do what you want. Please see the example in my answer. – stderr Mar 02 '11 at 15:13
  • even if you name the method __init(self,param)__ you can get the error if you do not indent the def and therefore the scope of the __init__ method is not in the class ... – Wolfgang Fahl Jun 12 '19 at 20:42

6 Answers6

65

In Python:

  • Instance methods: require the self argument.
  • Class methods: take the class as a first argument.
  • Static methods: do not require either the instance (self) or the class (cls) argument.

__init__ is a special function and without overriding __new__ it will always be given the instance of the class as its first argument.

An example using the builtin classmethod and staticmethod decorators:

import sys

class Num:
    max = sys.maxint

    def __init__(self,num):
        self.n = num

    def getn(self):
        return self.n

    @staticmethod
    def getone():
        return 1

    @classmethod
    def getmax(cls):
        return cls.max

myObj = Num(3)
# with the appropriate decorator these should work fine
myObj.getone()
myObj.getmax()
myObj.getn()

That said, I would try to use @classmethod/@staticmethod sparingly. If you find yourself creating objects that consist of nothing but staticmethods the more pythonic thing to do would be to create a new module of related functions.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
stderr
  • 8,567
  • 1
  • 34
  • 50
  • 4
    I thought that there are some points of your answer, that deserves a little bit of highlight. I hope you don't mind about the improvements :)! Thanks for the answer! – ivanleoncz May 25 '18 at 03:46
5

Every method needs to accept one argument: The instance itself (or the class if it is a static method).

Read more about classes in Python.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
4

The fact that your method does not use the self argument (which is a reference to the instance that the method is attached to) doesn't mean you can leave it out. It always has to be there, because Python is always going to try to pass it in.

kindall
  • 178,883
  • 35
  • 278
  • 309
2

In python you must always pass in at least one argument to class methods, the argument is self and it is not meaningless its a reference to the instance itself

Jordan
  • 4,928
  • 4
  • 26
  • 39
2

The current object is explicitly passed to the method as the first parameter. self is the conventional name. You can call it anything you want but it is strongly advised that you stick with this convention to avoid confusion.

neil
  • 3,387
  • 1
  • 14
  • 11
1

If you print(type(Num.getone)) you will get <class 'function'>.

It is just a plain function, and be called as usual (with no arguments):

Num.getone() # returns 1  as expected

but if you print print(type(myObj.getone)) you will get <class 'method'>.

So when you call getone() from an instance of the class, Python automatically "transforms" the function defined in a class into a method.

An instance method requires the first argument to be the instance object. You can think myObj.getone() as syntactic sugar for

Num.getone(myObj) # this explains the Error 'getone()' takes no arguments (1 given).

For example:

class Num:
    def __init__(self,num):
        self.n = num
    def getid(self):
        return id(self)

myObj=Num(3)

Now if you

print(id(myObj) == myObj.getid())    
# returns True

As you can see self and myObj are the same object

lazos
  • 1,035
  • 1
  • 7
  • 17