Static and Instance Attributes
Lesson 35 of 48 in Coddy's Python for PHP Developers course.
PHP static properties live on the class and use ::; instance properties use ->. Python class attributes are found through an instance when no same-named instance attribute exists. Assigning obj.category usually creates or changes an instance attribute, shadowing the class value. Assigning Class.category changes the shared class value. Mutating a list found through class lookup changes the shared list rather than creating an instance attribute.
PHP 8.3
<?php
class Shelf { public static $category = 'books'; }
echo Shelf::$category, "\n";
Shelf::$category = 'tools';
echo Shelf::$category, "\n";Python
class Shelf:
category = "books"
a = Shelf()
b = Shelf()
a.category = "tools"
print(a.category, b.category, Shelf.category)Porting trap
Do not treat every Python class-body assignment as a PHP instance-property declaration. Put independent tracks or balances on self inside initialization.
Complete the task in the editable Python files. Keep the required function and class names; the locked driver.py supplies execution.
Challenge
BeginnerImplement Playlist with class attribute category = "music", a fresh instance list tracks, and method add(track) that appends one string. Input has two space-separated track lines, either possibly empty, followed by a category label. The driver builds two playlists, fills them independently, and assigns the new category only to the first instance. Print each instance's category and comma-joined tracks as category:tracks, then Playlist.category. Track names have no commas or spaces.
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 playlist.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 playlist import Playlist
a, b = Playlist(), Playlist()
for track in input().split():
a.add(track)
for track in input().split():
b.add(track)
a.category = input()
print(f"{a.category}:{','.join(a.tracks)}")
print(f"{b.category}:{','.join(b.tracks)}")
print(Playlist.category)
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