0

can you please tell me what I'm doing wrong?

here is a simple code for a Python subclass to create a person, just adding the name, and I want another class that will tell me the movies that he has rented, i'm just at the begining now

class Person(object):
    def __init__(self,name):
        self.name = name

class Customer(Person):
    def __init__(self):
        self.movie = []
        super(Customer,self).__init__()

but I get this error when I try to use my code

Johnny = Customer("John")

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    Johnny = Customer("John"
TypeError: __init__() takes exactly 1 argument (2 given)

I'm new at Python and I don't really know what is going on!

2
  • Python is not C++; there is no function overloading. Why are you defining Person.__init__ twice? Commented Nov 6, 2014 at 21:12
  • Looks like a code block formatting problem. There's a newline missing somewhere in self.name = nameclass Customer(Person):. Commented Nov 6, 2014 at 21:12

2 Answers 2

2

Assuming your code actually looks like this:

class Person(object):
    def __init__(self,name):
        self.name = name
class Customer(Person):
    def __init__(self):
        self.movie = []
        super(Customer,self).__init__()

Johnny = Customer("John")

You should modify your Customer class initializer so it also takes a name parameter. Don't forget to pass it during super, too.

class Customer(Person):
    def __init__(self, name):
        self.movie = []
        super(Customer,self).__init__(name)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you!! I can vote you up because I don't have points, I'm sorry
1

You have defined the constructor for Person (Person.__init__) to take a single (non-self) argument named name:

class Person(object):
    def __init__(self,name):
        self.name = nameclass 

But when you call Person.__init__ from your derived class constructor Customer.__init__ you are not supplying a value for that parameter. Because Person requires name in its constructor, you need to supply a value for it when you invoke it.

class Customer(Person):
    def __init__(self,name):
        self.movie = []
        super(Customer,self).__init__(name)

2 Comments

Thank for your answer!!, but, correct me if I'm wrong, isn't it the whole idea of inheritance to re-use the super class parameters, not having to declare them again in the subclasses??
@randomguy04: The idea of inheritance, at least in Python, is that any attribute you define for a child class completely replaces the same-named attribute in the parent class (if it exists). There is nothing which forces the child's __init__() to even call the parent's __init__() at all! That's why you have to do it explicitly if you want that behavior. And that's why Python can't assume that the parameters are going to be the same.

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.