Explicit super Arguments
Lesson 39 of 48 in Coddy's Python for Ruby Developers course.
Ruby declares a subclass with a less-than sign; Python lists base classes in parentheses. Both can override inherited methods. Ruby bare super forwards the current method's arguments, while super() in Ruby supplies none. Python super() constructs a lookup proxy; call the intended method explicitly and supply its arguments. Python super follows the method resolution order, which matters for cooperative multiple inheritance, rather than always naming one direct parent. For the single-base exercises, call super().__init__ with the required base state and then initialize the new attributes. Inheriting a method does not automatically extend an overridden method's body.
Ruby
class Base
def label(text)
text + '!'
end
end
class Child < Base
def label(text)
super + '?'
end
end
puts Child.new.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
Do not port Ruby bare super as Python super() alone. The Python proxy must be used to invoke a method such as super().cost().
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 Ruby Developers
1From Ruby to Python
Run Your First Python ProgramStrings and InterpolationNames and Mutable StringsDivision and ParsingFrom end to IndentationRecap: Rewrite a Receipt4Iteration and Transformations
each and Python foreach_pair and enumeratenext Becomes continueselect, map, and sort_byRecap: Build a Ranking2Values and Decisions
nil Becomes None== and equal?Zero Is False in Pythonelsif and Conditional ValuesRecap: Eligibility Rules5Functions and Closures
Explicit Return ValuesDefaults Run at DefinitionSplat and Double SplatBlocks Become CallablesRecap: Refactor a Pricing Tool8Composition and Inheritance
Delegating to CollaboratorsExplicit super ArgumentsDuck Typing Without MixinsStruct and Dataclass EqualityRecap: Notification Channels3Collections and References
Array Methods and ListsParallel Assignment and TuplesHash Defaults and Dict Keysdup, Copies, and SetsRecap: Clean an Inventory6Reliable Python Programs
gets, input, and Formattingrescue and Context Managersrequire and Import GuardsHints Do Not Enforce TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler