Equatable and Identity
Lesson 8 of 48 in Coddy's Python for Swift Developers course.
Swift value types can implement Equatable, while class identity uses triple equals. Python equality uses double equals and identity uses is. Python lists compare elements; distinct equal lists are not the same object. Assigning a Python list creates another reference, unlike Swift Array assignment, which has value semantics with copy-on-write optimization. When comparing strings or numbers, use equality rather than identity because interning and caching can obscure identity mistakes.
Swift example
let a = ["red"]
let b = ["red"]
print(a == b)
final class Box {}
let first = Box()
let second = Box()
print(first === second)Python
a = ["red"]
b = ["red"]
print(a == b)
print(a is b)Porting trap
Use Python is None for singleton absence, not is with an ordinary numeric value.
Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.
Challenge
BeginnerRead two text labels on separate lines. The reserved token missing means an absent label; all other lines, including an empty line, are actual strings. The scaffold converts that token to None. Print whether the labels have equal values, then whether the first is absent, then whether the second is absent, one Python boolean per line.
Print only these lines, ending each with a newline. Keep the supplied input scaffold; it reads the stated values without prompts.
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 Swift Developers
1From Swift to Python
Run Your First Python ProgramPrinting and Stringslet, var, and RebindingInt Range and DivisionIndentation and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for and Half-Open Rangesenumerated to enumeratewhile and Loop Controlmap, filter, ComprehensionsRecap: Build a Ranking2Values and Decisions
Optionals and NoneEquatable and IdentityBool Conditions and FallbacksBranches and TernariesRecap: Eligibility Rules3Value Types to Shared Objects
Array to listTuples and UnpackingDictionary Lookup and OrderValue Copies and AliasesRecap: Clean an Inventory6Errors and Modules
Input and String Formattingthrows, defer, and withModules and Main GuardsHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler