New and Python Class Calls
Lesson 32 of 48 in Coddy's Python for GDScript Developers course.
GDScript instantiates script classes with new. Python calls the class directly. Ordinary Python classes can receive attributes dynamically; GDScript scripts generally declare members, with engine mechanisms for dynamic properties. Use pass for an empty Python class. Python objects here are plain data models, not Nodes, and need no scene-tree attachment or queue_free calls.
GDScript
extends SceneTree
class Parcel extends RefCounted:
var label = ""
func _init():
var p = Parcel.new()
p.label = "sample"
print(p.label)
quit()Python
class Parcel:
pass
p = Parcel()
p.label = "sample"
print(p.label)Porting trap
Do not assume an engine lifecycle will initialize a Python model. Create its attributes deliberately and call its methods through ordinary application code.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
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 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