Concepts / Namespaces and Scope

Namespaces and Scope

Fields are ordinary variables bound to class or object namespaces, scoped to their context and not global.

  • Programming

Two Kinds of Ownership

A field is an ordinary variable bound to a namespace. In a class design, that namespace belongs either to the class itself or to one individual object. This ownership decision controls whether the value is shared or separate. Class variables have one shared copy for all instances, while object variables have an independent copy for each instance.

Ask who owns the data: the class as a whole, or each individual instance? That question usually tells you which variable type to use.

ownsownsownsRobot classclass namespacepopulationshared fieldrobot1object namespacenameR2D2robot2object namespacenameC3PO
What contains a class variable versus an object variable, and how are those namespaces connected?

Shared and Separate Values

A class variable is owned by the class. There is one copy, so every instance sees the same value. If that shared value changes, the change is visible to all instances. An object variable is owned by one instance. Each instance has its own independent copy, so changing one object's value does not change the corresponding value in another object.

QuestionClass variableObject variable
Who owns it?The classOne individual instance
How many copies exist?One shared copyOne independent copy per instance
Who sees a change?All instancesOnly the instance whose value changed
How is it accessed?Through the class name, such as Robot.populationThrough an instance, such as robot1.name
visible tovisible topopulationone shared valuerobot1sees populationnameR2D2robot2sees populationnameC3PO
Which values are shared by every instance, and which values exist separately for each object?

Robot Population Trace

The source example uses a Robot class with a class variable named population and an object variable named name. The class-level definition population = 0 creates the shared count. Inside initialization, self.name = name creates a name field for the individual object. The population count is increased when a robot is created and decreased when robot1.die() runs.

Following Two Robots

Two robots are created: robot1 with the name R2D2 and robot2 with the name C3PO. Then robot1 dies. Determine the population and each robot's name after each important step.

Initial class state: The class variable population begins at 0. No robot-specific name has been created yet.

Create robot1: The initialization process creates robot1's object variable name with the value R2D2. The shared class variable population increases to 1.

Create robot2: A separate object variable name is created for robot2 with the value C3PO. The same shared population variable increases to 2. The two name values remain independent.

Remove robot1: The die operation decreases the shared class variable population to 1. It does not change robot2's object variable name.

After robot1 dies, the shared population is 1. robot1's name is R2D2 and robot2's name is C3PO; the object variable belonging to robot2 is unaffected.

robot1.die()unchangedunchangedpopulation2population1robot1.nameR2D2robot1.nameR2D2robot2.nameC3POrobot2.nameC3PO
What changes after modifying a class variable compared with modifying one object's variable?

Reading Variable Access

The access form identifies the owner. A class variable is accessed through the class name, as in Robot.population. An object variable is accessed through an instance, as in robot1.name. When tracing code, first identify the name before the dot, then ask whether that name refers to the class or to one object. That tells you which namespace contains the field and who will observe a change.

inspect prefixclass nameinstance namechange is sharedchange is independentVariable accessOwner nameclass or instanceClass namespaceShared valuevisible to all instancesObject namespaceObject valuevisible to one instance
When code accesses or modifies a variable, which namespace is searched and where does the change occur?
  1. Find the variable access, such as Robot.population or robot1.name.
  2. Identify the owner named before the dot.
  3. Classify the owner as the class or as one individual instance.
  4. Predict who will see a change: all instances for a class variable, or only the owning instance for an object variable.
  5. Check whether the operation changes the shared class value or the selected object's independent value.

Choosing the Right Owner

Choose a class variable when the data logically belongs to the class as a whole and every instance should see the same value. A count of all objects is the source example's use case. Choose an object variable when each instance needs its own independent value, such as an individual object's name or state.

Data requirementAppropriate variable typeReason
One count shared by all objectsClass variableThe class owns one shared value.
A separate name for each objectObject variableEach instance owns its own value.
A value that every instance must see after one shared updateClass variableChanges are visible to all instances.
A value that one object can change without affecting othersObject variableEach instance has an independent copy.

Mistakes in Scope Tracing

  • Assuming every field belongs separately to each object.

    population is a class variable with one shared copy.

    Fix: Trace the owner named before the dot. Robot identifies the class namespace, so every instance sees the shared population value.

  • Assuming an object variable is shared because several objects use the same field name.

    The same field name can exist in separate object namespaces.

    Fix: Treat robot1.name and robot2.name as independent object variables belonging to different instances.

  • Ignoring the access form.

    The owner indicates whether the field is attached to the class or to one object.

    Fix: Inspect the name before the dot, then predict who observes a modification.

  • Choosing a variable type without considering ownership.

    A class variable gives all instances the same value and exposes shared changes.

    Fix: Use an object variable when each instance requires an independent value.

Practice the Ownership Test

EASY

A class needs to store the total number of objects created and a separate label for each object. Decide which data should be a class variable and which should be an object variable. Then predict what happens to the other objects when one object's label changes and when the total count changes.

Hints
  • Ask whether the total belongs to the class as a whole or to one object.
  • Ask whether each object needs an independent label.
  • Use the owner before the dot to predict who sees each change.

What do you think happens?

Two Robot objects exist. If the shared population value is decreased when one robot dies, should the other robot see the new population value?

  • Yes, because population belongs to the class.
  • No, because every object has its own population value.
  • Only if the other robot's name changes.
Reveal answer

Answer: Yes, because population belongs to the class.

A class variable has one shared copy, so a change to it is visible to all instances. The other robot's individual name remains unaffected because name is an object variable.

Key Takeaways

  • Fields are variables bound to either a class namespace or an object namespace; they are not global by default.
  • Class variables are shared: one copy exists, and all instances see changes to it.
  • Object variables are independent: each instance has its own copy, and changes affect only that instance.
  • The owner before the dot helps identify the namespace: use the class name for class variables and the instance name for object variables.
  • Choose the variable type by asking whether the data belongs to the class as a whole or to each individual object.