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
61.5k14 gold badges134 silver badges194 bronze badges
asked Mar 2, 2011 at 14:55
3
  • 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. Commented Mar 2, 2011 at 15:01
  • 2
    You can use @staticmethod and @classmethod decorators to do what you want. Please see the example in my answer. Commented Mar 2, 2011 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 ... Commented Jun 12, 2019 at 20:42

6 Answers 6

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
10.3k7 gold badges62 silver badges53 bronze badges
answered Mar 2, 2011 at 15:11
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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.

answered Mar 2, 2011 at 14:58

Comments

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.

answered Mar 2, 2011 at 15:01

Comments

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

answered Mar 2, 2011 at 14:58

Comments

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.

answered Mar 2, 2011 at 15:04

Comments

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

answered Jun 25, 2020 at 16:31

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.