Equality and Object Identity
Lesson 8 of 48 in Coddy's Python for Rust Developers course.
Rust == invokes PartialEq, often derived or implemented for value equality. Python == likewise asks the objects for equality behavior, whereas is tests whether they are the same object. Separately constructed Python lists can have equal contents and distinct identities. Rust let b = a moves a non-Copy vector; Python b = a leaves both names usable and points them at one list. Use identity for singleton absence checks, not to compare numbers or strings by value.
Rust excerpt inside main
let a = vec![String::from("red")];
let b = a.clone();
println!("{}", a == b);
println!("{}", std::ptr::eq(&a, &b));Python
a = ["red"]
b = ["red"]
print(a == b)
print(a is b)Porting trap
Rust clone of a vector of owned strings duplicates those values. Python assignment shares the list; copying requires an explicit operation and may still be shallow.
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 Rust Developers
1From Rust to Python
Run Your First Python ProgramMacros to printBindings, mut, and ShadowingIntegers and Floor DivisionIndentation and ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Borrowed Iteration to forEntries and enumeratewhile, break, and continueIterators and ComprehensionsRecap: Build a Ranking2Values and Decisions
Option and NoneEquality and Object IdentityTruth Tests and FallbacksIf Expressions and elifRecap: Eligibility Rules3Ownership and Collections
Vec to listTuples and DestructuringHashMap to dictClone and Shallow CopiesRecap: Clean an Inventory6Errors and Modules
Text Input and FormattingResult, Exceptions, and withModules and the Main GuardHints Without Borrow CheckingRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler