Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The inheritance of attributes using __init__

I'm Java person who just started learning Python. Take this example:

class Person():     def __init__(self, name, phone):         self.name = name         self.phone = phone  class Teenager(Person):     def __init__(self, name, phone, website):         self.name=name         self.phone=phone         self.website=website 

I'm sure there's a lot of redundant code (I know in Java, there are a lot of redundancies for the bit of code above).

Which parts are redundant with respect to which attributes are already inherited from the parent class?

like image 712
user1111042 Avatar asked Jan 13 '12 16:01

user1111042


People also ask

What is the use of __ init __ in Python?

The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.

Are attributes inherited in Python?

Inheritance is when a class is created based on an existing class, and the new class inherits the attributes and methods from the existing class. The new class is usually called “child class”, and the existing class is called “parent class”.

What is __ init __( self in Python?

The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.

What is attribute inheritance?

An inherited attribute is one that is inherited from a parent product class. You customize an inherited attribute domain by editing its definition at the subclass level. When you edit an inherited attribute definition, the changes propagate to all members of the subclass, including other subclasses under that subclass.


1 Answers

When writing the __init__ function for a class in python, you should always call the __init__ function of its superclass. We can use this to pass the relevant attributes directly to the superclass, so your code would look like this:

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

As others have pointed out, you could replace the line

Person.__init__(self, name, phone) 

with

super(Teenager, self).__init__(name, phone) 

and the code will do the same thing. This is because in python instance.method(args) is just shorthand for Class.method(instance, args). If you want use super you need to make sure that you specify object as the base class for Person as I have done in my code.

The python documentation has more information about how to use the super keyword. The important thing in this case is that it tells python to look for the method __init__ in a superclass of self that is not Teenager

like image 143
murgatroid99 Avatar answered Nov 07 '22 08:11

murgatroid99