TypeError: method() takes exactly 2 arguments (3 given)

Question:

Here is a code:

class Child(object):
    def chunks(l, n):
        """ Yield successive n-sized chunks from l.
        """
        for i in xrange(0, len(l), n):
            yield l[i:i+n]

k= range(1, 10)
print k
print Child().chunks(k,2)

When I execute this code, python throws following error:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Traceback (most recent call last):

File
“/home/Sample.py”, line 19, in

print Child().chunks(k,2)

TypeError: chunks() takes exactly 2 arguments (3 given)

Please find my snippet !

Asked By: Jay Venkat

||

Answers:

Instance method: A method that is defined inside a class and belongs only to the current instance of a class.

Define chunks method as instance method in class.

e.d

class Child(object):
    def chunks(self, l, n):
        #      ^^^   
        pass
        # do coding

Static Method:

class Child(object):
    @staticmethod
    def chunks(l, n):
        pass
        # do coding
Answered By: Vivek Sable
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.