Static and Instance State
Lesson 35 of 48 in Coddy's Python for GDScript Developers course.
GDScript static variables provide shared class state while ordinary members belong to instances. Python class attributes are also shared through lookup, but assigning the same name on an instance can shadow the class value. Mutating a shared class-level list changes that one list instead of shadowing it. Store independent tracks or balances on self during initialization.
GDScript
extends SceneTree
class Shelf extends RefCounted:
static var category = "books"
func _init():
print(Shelf.category)
Shelf.category = "tools"
print(Shelf.category)
quit()Python
class Shelf:
category = "books"
a = Shelf()
b = Shelf()
a.category = "tools"
print(a.category, b.category, Shelf.category)Porting trap
A Python class-body assignment is not the same as an ordinary GDScript instance member declaration. This is a common source of accidental shared state in ports.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
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 GDScript Developers
1From GDScript to Python
Run Your First Python ProgramPrint Is Not printsVar, Const, and BindingsInteger Division DifferencesSimilar Indentation, New ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and RebindingItems and enumerateWhile and Early ExitArray Callbacks and KeysRecap: Build a Ranking7Instances and Shared State
New and Python Class CallsInit and Per-instance ListsCallable and Bound MethodsStatic and Instance StateSetters and Printable ObjectsRecap: Model a Bank Account2Values and Decisions
Null and NoneThe is Keyword Changes MeaningLogical Results and OperandsConditional ExpressionsRecap: Eligibility Rules3Collections and Shared State
Array and List APIsArrays, Vectors, and TuplesDictionary Lookup RulesDuplicate and Shallow CopyRecap: Clean an Inventory6Input, Errors, and Modules
Console Work Outside GodotValidation and ExceptionsScript Resources and ImportsTyped GDScript and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler