extends and Method Resolution
Lesson 39 of 48 in Coddy's Python for Dart Developers course.
Dart extends declares a base class and super invokes inherited behavior. Python lists base classes in parentheses and uses super() as a proxy that continues lookup according to its method resolution order. Call a named method on that proxy and supply required arguments. For a single-base initializer, super().__init__ establishes inherited state before adding subclass state. Python supports multiple inheritance, so the proxy should not be understood as a hard-coded direct-parent reference in every hierarchy. An override replaces ordinary lookup of the inherited method unless it explicitly delegates to it.
Dart
class Base {
String label(String text) => '$text!';
}
class Child extends Base {
@override
String label(String text) => '${super.label(text)}?';
}
void main() { print(Child().label('go')); }Python
class Base:
def label(self, text):
return text + '!'
class Child(Base):
def label(self, text):
return super().label(text) + '?'
print(Child().label('go'))Porting trap
A Python super() expression alone does not call a constructor or method. Follow it with the intended method call.
Challenge
BeginnerImplement Pass(label, price) with stored attributes and cost() returning the integer price. Implement subclass DiscountPass(label, price, discount), initializing base state through super().__init__ and storing discount. Override cost() to return the base cost minus the discount, clamped at zero. Input is label, nonnegative price, and nonnegative discount on separate lines. The driver prints the label and cost for a regular pass and a discounted pass, then increases the discounted pass's price by 10 and prints its new cost. Calculate from current state.
The locked driver handles input and output. Submit only the required class and function definitions with the exact API names. Do not read input or print from your classes.
The locked driver also verifies the required subclass relationship at runtime.
Your code goes in the editable files pass_model.py and discount_pass.py. driver.py holds the supplied input/output code and API checks and cannot be edited. Keep the supplied imports; do not read input or print from the editable modules.
Try it yourself
from pass_model import Pass
from discount_pass import DiscountPass
assert issubclass(DiscountPass, Pass), "Required subclass relationship missing"
label = input()
price, discount = int(input()), int(input())
a, b = Pass(label, price), DiscountPass(label, price, discount)
print(a.label, a.cost())
print(b.label, b.cost())
b.price += 10
print(b.cost())
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules5Functions and Closures
Arrows and Explicit ReturnsNamed Calls and DefaultsExplicit Variadic ArgumentsClosures and nonlocalRecap: Refactor a Pricing Tool8Composition and Inheritance
Collaborator Objectsextends and Method ResolutionInterfaces and Duck TypingValue Classes and DataclassesRecap: Notification Channels3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler