Instances Across Modules
Lesson 32 of 48 in Coddy's Python for C# Developers course.
Construct independent instances across real Python modules.
C# reference excerpt
Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.
var parcel = new Parcel();
parcel.Label = "box";
Console.WriteLine(parcel.Label);
// Type declaration follows top-level statements.
class Parcel { public string Label = ""; }Python
class Parcel:
pass
parcel = Parcel()
parcel.label = "box"
print(parcel.label)What changes
Ordinary Python instances can acquire attributes after creation, unlike adding undeclared fields to an ordinary C# class instance. pass is an empty suite placeholder. Each Parcel() creates a fresh instance. In the task, parcel.py defines Parcel and functions.py uses from parcel import Parcel. The provided imports connect real files; driver.py imports your factory and handles input. Keep one class in its named file.
Transfer mistake
Class names and instances are different objects. Calling Parcel() creates an instance; returning Parcel itself returns the class. Dynamic attributes are a feature of these ordinary classes, not a guarantee for every Python type.
Your task
Define an empty Parcel class with pass in parcel.py. In functions.py implement make_parcel(label, weight): create a fresh instance, assign label and weight attributes, and return it. Five input lines hold two label/weight pairs and an increase. Labels may be empty; integers are nonnegative. The driver increases only the first parcel's weight, prints both label:weight lines, and prints their identity comparison.
Challenge
BeginnerDefine an empty Parcel class with pass in parcel.py. In functions.py implement make_parcel(label, weight): create a fresh instance, assign label and weight attributes, and return it. Five input lines hold two label/weight pairs and an increase. Labels may be empty; integers are nonnegative. The driver increases only the first parcel's weight, prints both label:weight lines, and prints their identity comparison.
Editable implementation files: parcel.py, functions.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Keep the supplied imports and exact API names. Return results or raise the required exceptions; do not read input or print from these implementation modules.
Try it yourself
from parcel import Parcel
from functions import make_parcel
a = make_parcel(input(), int(input()))
b = make_parcel(input(), int(input()))
assert isinstance(a, Parcel) and isinstance(b, Parcel)
a.weight += int(input())
print(f"{a.label}:{a.weight}")
print(f"{b.label}:{b.weight}")
print(a is b)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking7Objects and Properties
Instances Across ModulesInitialize Instance StateBound Methods and selfClass and Instance AttributesProperties and Access RulesRecap: Independent Accounts2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler