0

I have a file notifications.py and comments.py. notifications has a class GetNotifications with a function getNotifs(arg1, arg2). I'd like to call this method in my comments.py, so I do:

from notifications import GetNotifications

Then I create an instance of the class:

getNotifsInstance = GetNotifications()

Then I try to call getNotifs:

notifsDictionary = getNotifsInstance.getNotifs(arg1, arg2)

However, I'm getting the error: TypeError: getNotifs() takes at most 2 arguments (3 given)

Why is it saying I'm giving it 3 arguments when I'm only giving it 2?

Marcin
50.1k18 gold badges137 silver badges207 bronze badges
asked Apr 17, 2012 at 15:06
1
  • 1
    When you call a method of an instance it always passes itself as the first parameter. Commented Apr 17, 2012 at 15:08

4 Answers 4

3

You're giving it three arguments: when you call an instance method, the instance is passed as the first parameter. You should add an argument self as the first parameter of the method.

answered Apr 17, 2012 at 15:08
Sign up to request clarification or add additional context in comments.

Comments

3

You've most probably forgotten about the self argument when declaring getNotifs():

def getNotifs(self, arg1, arg2):
 ...
answered Apr 17, 2012 at 15:09

Comments

1

Why is it saying I'm giving it 3 arguments when I'm only giving it 2?

Simply because you are accessing the function getNotifys as a member function of object getNotifsInstance. The first argument to any member function is (self) the object reference itself.

answered Apr 17, 2012 at 15:09

Comments

1

In the class you can define three kinds of methods:

class A(object):
 def instance_method(*args):
 print '%d arguments given' % len(args)
 @classmethod
 def class_method(*args):
 print '%d arguments given' % len(args)
 @staticmethod
 def static_method(*args):
 print '%d arguments given' % len(args)

And when you invoke them on the instance, you will get additional argument passed to instance method (which will be instance itself) and class method (which will be a class of the instance):

>>> a = A()
>>> a.instance_method('a', 'b')
3 arguments given
>>> a.class_method('a', 'b')
3 arguments given
>>> a.static_method('a', 'b')
2 arguments given

In your case it is probably self (the instance itself), although results would be similar if you would decorate your method with classmethod decorator (in that case you would get a class passed as the first argument).

answered Apr 17, 2012 at 15:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.