Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Using super()

Once in a while, you need to access parent class methods and attributes from within the instance—for example, when you initiate a new one and need to execute the parent class's initialization logic. For that, we can use a built-in function called super. It is very easy to use—just call it from within a class—and it will return a proxy object of a direct parent with access to all its methods and attributes. Take a look at the following snippet:

class Shark(Fish):  def __init__(self, w=5000, teeth=121): self.teeth = teeth super().__init__(w=w)

Here, we create a new class called Shark, inheriting from Fish. This new class has its own custom initialization code, but it also utilizes the __init__ method of the Fish class via the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content