Skip Huffman
Skip Huffman

Reputation: 5449

Python Object definition

This should be easy, but for I am missing something.

I have an object that works exactly as I expect.

class TextElement(ContentItemElement):
    '''
    Single String Elements, for example, headlines
    '''
    def __init__(self, name, data):
       super(TextElement, self).__init__()
       self.name=name
       self.text=data


    def prettyPrint(self):
        printstring =  u'*HTML* '
        self.name.encode('utf-8')
        printstring += u'<h3> '+self.name+u' </h3>'
        self.text.encode('utf-8')
        printstring += u'<p> '+self.text+u' </h3>'
        print printstring 

Ok, great, I can instantiate that, and it does exactly what I want it to. But I really would like to create a more specific version of TextObjects. so I do this:

class CiteElement(TextElement):
    '''
    Single String Elements, for example, headlines
    '''
    def __init__(self, name, data):
        super(CiteElement, self).__init__()
        self.validValues=['Crap I make up', 'Crap I found on the web']

but when I try to instantiate it, this works:

ee = TextElement(element, self.raw[element])
ee.validValues=['Crap I make up', 'Crap I found on the web']

but this Does not

ee = CiteElement(element, self.raw[element])

Instead giving me this error:

TypeError: __init__() takes exactly 3 arguments (1 given)

Obviously I am missing something small. Something key to python objects. Something I should darn well know, but have been coding around for years. But what is it?

Upvotes: 1

Views: 287

Answers (2)

user2665694
user2665694

Reputation:

Because the constructor of your base class is defined as

def __init__(self, name, data):
....

And you are calling it without parameters from your derived class.

def __init__(self, name, data):
    super(CiteElement, self).__init__()

Upvotes: 3

Michael Lorton
Michael Lorton

Reputation: 44376

This line

super(CiteElement, self).__init__()

should be

super(CiteElement, self).__init__(name, data)

Upvotes: 5

Related Questions