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.

Key point

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

Robot classclass namespacepopulationshared fieldrobot1object namespacenameR2D2robot2object namespacenameC3POownsownsowns
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

Ownership determines whether a field is shared or independent.

populationone shared valuerobot1sees populationnameR2D2robot2sees populationnameC3POvisible tovisible to
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.

Worked example

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.

  1. 1
    Initial class state

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

  2. 2
    Create robot1

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

  3. 3
    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.

  4. 4
    Remove robot1

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

Result

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.

population2population1robot1.nameR2D2robot1.nameR2D2robot2.nameC3POrobot2.nameC3POrobot1.die()unchangedunchanged
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.

Variable accessOwner nameclass or instanceClass namespaceShared valuevisible to all instancesObject namespaceObject valuevisible to one instanceinspect prefixclass nameinstance namechange is sharedchange is independent
When code accesses or modifies a variable, which namespace is searched and where does the change occur?
  1. 1
    Find the variable access, such as Robot.population or robot1.name.
  2. 2
    Identify the owner named before the dot.
  3. 3
    Classify the owner as the class or as one individual instance.
  4. 4
    Predict who will see a change: all instances for a class variable, or only the owning instance for an object variable.
  5. 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.

Match the variable type to the ownership requirement.

Mistakes in Scope Tracing

  • Common mistake

    Assuming every field belongs separately to each object.

    What it looks like

    Treating Robot.population as if robot1 and robot2 each had unrelated population values.

    Why it fails

    population is a class variable with one shared copy.

    Do this instead

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

  • Common mistake

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

    What it looks like

    Assuming robot1.name and robot2.name must refer to one common name value.

    Why it fails

    The same field name can exist in separate object namespaces.

    Do this instead

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

  • Common mistake

    Ignoring the access form.

    What it looks like

    Reading robot1.name and Robot.population without using the owner to classify the field.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Choosing a variable type without considering ownership.

    What it looks like

    Using a shared class variable for data that should differ for every object.

    Why it fails

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

    Do this instead

    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.

Related concepts

More concepts starting with N