Instances Without new
Lesson 32 of 48 in Coddy's Python for PHP Developers course.
PHP creates an object with new and accesses properties with ->. Python calls the class itself and uses a dot for attributes. An ordinary empty Python class uses pass as its body and permits instance attributes to be assigned afterward. In modern PHP, declaring properties avoids dynamic-property deprecations. This difference is not a reason to omit a model's intended state: later Python lessons initialize attributes deliberately.
PHP 8.3
<?php
class Parcel { public $label; }
$p = new Parcel();
$p->label = 'sample';
echo $p->label, "\n";Python
class Parcel:
pass
p = Parcel()
p.label = "sample"
print(p.label)Porting trap
Assignment shares an existing object in both examples' object models; neither b = a nor PHP $b = $a constructs a new independent instance. Create another instance when you need independent state.
Complete the task in the editable Python files. Keep the required function and class names; the locked driver.py supplies execution.
Challenge
BeginnerDefine an empty Parcel class using pass as its class body. Define make_parcel(label, weight) to create a fresh Parcel(), assign instance attributes label and weight, and return it. Input has five lines: first label, first integer weight, second label, second integer weight, and an integer increase. Labels may be empty; weights and increase are nonnegative. The driver increases only the first parcel's weight, then prints each parcel as label:weight, followed by whether the two parcels are identical (True or False).
The locked driver reads input and prints results. Submit the required class and function definitions only; do not read input or print in your solution. Keep the API names exactly as specified.
Your code goes in the editable files parcel.py and functions.py. driver.py holds the supplied input/output code and API checks and cannot be edited. Keep the supplied imports; do not read input or print from the editable 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 PHP Developers
1From PHP to Python
Run Your First Python ProgramEcho, Strings, and printVariables Without Dollar SignsDivision and Numeric TextIndentation and SuitesRecap: Rewrite a Receipt4Iteration and Transformations
Foreach and rangeKeys, Items, and enumerateWhile Without IncrementFilters and Sort KeysRecap: Build a Ranking7Instances and Shared State
Instances Without newConstructors and Fresh ListsThis and Bound MethodsStatic and Instance AttributesProperties and Object TextRecap: Model a Bank Account2Values and Decisions
Null, None, and Missing KeysStrict Equality Is Not isThe Zero String TrapElif and Value SelectionRecap: Eligibility Rules3Arrays and Python Collections
PHP Arrays and Python ListsDestructuring and TuplesKeys, Null, and getArray Copies and List AliasesRecap: Clean an Inventory6Input, Errors, and Modules
CLI Input and Exact OutputConversion Errors and CleanupImports Instead of requireType Hints Are Not PHP TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler