var, final, and Python Names
Lesson 3 of 48 in Coddy's Python for Dart Developers course.
Dart var infers a static type, so a variable inferred as int cannot later hold a string. Python names have no declared fixed type and can be rebound to objects of different types. The objects themselves still have runtime types. Dart final prevents rebinding the variable but does not freeze a mutable object it references. Dart const creates compile-time constants with stronger immutability rules for constant collections. Python uppercase names are only a convention and have neither final nor const enforcement. Assignment shares a reference to a list in both languages. Python string rebinding leaves other names bound to the old immutable string.
Dart
void main() {
final values = <int>[1];
final alias = values;
alias.add(2);
print(values);
}Python
label = 'red'
alias_label = label
label = 'blue'
print(alias_label)Porting trap
Do not treat an uppercase Python name as a Dart final declaration. Code can still rebind it unless your application enforces another rule.
Challenge
BeginnerRead two lines: an initial status and a replacement status. Keep the initial value in original, reassign status to the replacement, then print the original value and current value on separate lines. Both lines can be empty. The input scaffold reads the replacement into replacement.
Print only the requested output, with a newline after each line. The supplied input lines read the test data; keep them and add your work below.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
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 Rules3Collections 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