-2

I have a class Query:

class Query
    def __init__(self, keyword, **kwargs):
      self.keyword = keyword
      self.parameters = kwargs

    def __repr__(self):
       return "Query keyword %s, params %s" % (self.keyword, self.parameters)

Good so far. Now I created a class that inherits from it:

class SimpleQuery(Query):
    def __init__(self, keyword, count, age):
        Query(keyword, count, age)

And if I create an instance, I get...

>>> m = SimpleQuery(keyword, count=120, age=100)
TypeError: __init__() takes exactly 2 arguments (4 given)

What I would expect, of course, is for it to return an object along the lines of "Query keyword keyword, params {count: 120, age: 100}." What am I doing wrong?

falsetru
  • 357,413
  • 63
  • 732
  • 636
Chris vCB
  • 1,023
  • 3
  • 13
  • 28
  • possible duplicate of [Call a parent class's method from child class in Python?](http://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class-in-python) – simonzack Sep 21 '14 at 16:40

1 Answers1

3

To call superclass method:

class SimpleQuery(Query):
    def __init__(self, keyword, count, age):
        super(SimpleQuery, self).__init__(keyword, count=count, age=age)

If you use Python 3.x:

class SimpleQuery(Query):
    def __init__(self, keyword, count, age):
        super().__init__(keyword, count=count, age=age)

UPDATE

If you use Python 2.x and if the Query class id old-style class, do as follow:

class SimpleQuery(Query):
    def __init__(self, keyword, count, age):
        Query.__init__(self, keyword, count=count, age=age)
falsetru
  • 357,413
  • 63
  • 732
  • 636