Menu
CoddyTech

Objects and Instances

Lesson 32 of 48 in Coddy's Python for JavaScript Developers course.

Create independent Python objects to keep related values together. Object-oriented programming groups state with the operations that use it; an instance is one concrete object created from a class. A class gives those instances a shared definition, while each instance can hold its own data.

JavaScript

class Lamp {}
const desk = new Lamp();
desk.color = "blue";
const shelf = new Lamp();
shelf.color = "red";
console.log(desk.color, shelf.color);

Python

class Lamp:
    pass

desk = Lamp()
desk.color = "blue"
shelf = Lamp()
shelf.color = "red"
print(desk.color, shelf.color)

What changes

Both complete examples print blue red. Python calls the class without new. The pass statement supplies an empty class body without doing work. Assigning desk.color stores an attribute on that instance. JavaScript also permits object literals such as {color: "blue"}; Python's dictionary syntax creates a dictionary, not an instance of your class. For a dictionary use key lookup; for these instances use attribute access. You will move setup into an initializer in the next lesson.

Common mistake

Assigning shelf = desk would give two names to one lamp. It would not make a second instance. Construct a new object when independent state is required. Two objects can contain identical values while still being different objects, so use identity only to ask whether references name the same instance.

Your task

Create parcels with independent attributes, then observe how changing one parcel leaves another unchanged. The locked driver supplies input and checks your class and factory function.

Working across Python files

Each Python file is a module. Here, parcel.py holds Parcel, and functions.py holds make_parcel. The line from parcel import Parcel makes that class name available inside functions.py; omit .py in an import. Names in another module are not automatically visible. This is similar to importing a named export in JavaScript, but Python needs no export declaration for these definitions.

The program starts in the locked driver.py, which imports your definitions, reads the test input once, and prints the results. Imported modules define classes and functions without reading input themselves. Keep one learner class per file and free helper functions in functions.py. All required imports and the driver are provided as scaffolding: you are practicing objects, not expected to invent an advanced module layout. Edit the class or function named by the task and keep the supplied imports.

challenge icon

Challenge

Beginner

Define 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for JavaScript Developers

Practice on your own: Online Python compiler