Menu
CoddyTech

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 icon

Challenge

Beginner

Read 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__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Dart Developers

Practice on your own: Online Python compiler