Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ashley Keeling
11,476 Pointsi don't get class's in python
i dont quite get class's, i get parts of it but not the more advanced stuff
i dont fully understand how super class's work what does parent class mean?
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsA class is an object that can inherit from other classes. The class inherited from is the "parent" and the class being defined is a "sub-class" of the parent. If a parent class is not explicitly listed in the class definition, then the built-in object class is used as the base parent class.
When inheriting from a parent class, everything is included: attributes, methods, properties, etc. The sub-class can then add additional new functionality or override the existing functionality of the parent by creating attributes, methods, or properties of the same name as the parent. When an attribute, method, or property is referenced in the sub-class, the local sub-class definition is first check for an override of the parent functionality. If it is not found locally in the sub-class definition, then the parent class is automatically checked for reference.
But what happens if you only want to extend some method of a parent class without having to completely replicate the entire parent method locally within the sub-class? This is where super() is used.
super() says skip checking the local sub-class namespace for the referenced method and, instead, skip immediately to the parent class to find the reference. If there are more than one parent class (aka multiple inheritance), then search each parent in the order listed (left-to-right) in the sub-class definition. Without super you could not override the parent functionality and also still reference the parent functionality.
The super() statement need not be the last statement in a method. Sometimes you may wish to run the parent's method first, then make local modifications to the sub-class object.
A common usage of super() is in the __init__ method. For example:
class Parent:
def __init__(self):
# define a local (to Parent) attribute
self.parent_attribute = True
class SubClass(Parrent):
# this __init__ overrides the __init__ found in the Parent class
def __init__(self):
# define a local attribute
self.subclass_attribute = True
# call the parent class __init__ method to complete the initialization
super().__init__()
# running in the shell to see instance attributes
>>> Parent().__dict__
{'parent_attribute': True}
>>> SubClass().__dict__
{'subclass_attribute': True, 'parent_attribute': True}
Post back if you have more questions. Good luck!!