1

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?

3
  • 1
    fix your indentation of your code inside the class Commented Jan 15, 2013 at 16:17
  • Sorry, that's stack, not my code. Fixed. Commented Jan 15, 2013 at 16:19
  • @SkipHuffman -- Not quite fixed. Your docstrings are still out of alignment. Commented Jan 15, 2013 at 16:22

2 Answers 2

5

This line

super(CiteElement, self).__init__()

should be

super(CiteElement, self).__init__(name, data)
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, because I need to pass that information on to the parent object?
Yup. It needs those for parent-init; it cannot make the connection just because the parameters have the same name.
What you need to pass to your parent is up to you...you are designing your application. When your parent constructor requires parameters then you need to pass them, obviously.
Right, that is what I was asking. I knew I was close to what I wanted.
3

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__()

2 Comments

I never win these answer-races! In your face! In your face!
Yeah, but yours has a bit more detail of what mistake I made, not just how to correct it.

Your Answer

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.