5

Edit: This is a silly mistake, look at the answers, but my init method does not define a self variable.

I have several python classes that are stages in a pipeline. They inherit from a base class.

class StageBase(object):
    key = Segments.NONE
    _preprocessors = []

    def __init__():
        self.closing = False
        self.working = False
        self._que = None
        self._working_lock = None
        self._que_lock = None

    #whole bunch of other methods

The inheriting classes override key and _preprocessors. Before adding the explicit __init__() method, everything worked fine, now I get the following error:

TypeError: __init__() takes no arguments (1 given)

The line of the error is the line where I override _preprocessors (in this example, this class variable represents other stages that should be executed before this stage).

An example class that throws this error:

class StaticPageStage(StageBase):
    key = Segments.STATICPAGE
    _preprocessors = [FacebookGraphStage(), ]

    def __init__():
        pass

    def process(self, data):
        data = self._preprocess(data)
        return self.handle_results(tuple(x for x in data))

Can anyone tell me how to fix this?

marr75
  • 5,637
  • 1
  • 24
  • 41
  • I was about to change the title of your question, as your problem is not related to it, but I wanted to avoid being too invasive as I should have to completely rephrase it.. Please change it by yourself. :) – Andrea Spadaccini Apr 06 '11 at 16:17
  • I'm juggling a bunch of syntaxes lately. Silly mistake. – marr75 Apr 06 '11 at 16:29
  • Anybody know of a similar question to tag as duplicate? – marr75 Apr 06 '11 at 16:43
  • 1
    we all make errors. :) Here's a possible duplicate: http://stackoverflow.com/questions/4445405/python-compiler-error-x-takes-no-arguments-1-given – Andrea Spadaccini Apr 06 '11 at 22:11

3 Answers3

8

You need to add the default self parameter to __init__:

def __init__(self):
Andrea Spadaccini
  • 12,000
  • 5
  • 39
  • 54
2

init must have at least one argument in its signature:

def __init__(self):
    pass

Here the error comes probably from FacebookGraphStage. Check its init method (and the other ones in your files) to solve the problem

Simon Bergot
  • 9,930
  • 7
  • 37
  • 54
1

You always use the self argument within function definitions inside of classes, even if the function never takes in a parameter. self is passed to all of the functions as a first parameter, so the error is saying that your function __init__ isn't accepting it.

Here's some (hopefully) working code:

class StageBase(object):
    key = Segments.NONE
    _preprocessors = []

    def __init__(self):
        self.closing = False
        self.working = False
        self._que = None
        self._working_lock = None
        self._que_lock = None

    #whole bunch of other methods


class StaticPageStage(StageBase):
    key = Segments.STATICPAGE
    _preprocessors = [FacebookGraphStage(), ]

    def __init__(self):
        pass

    def process(self, data):
        data = self._preprocess(data)
        return self.handle_results(tuple(x for x in data))
Blender
  • 275,078
  • 51
  • 420
  • 480