215 Variable scope in OOP

  1. In procedural programming, what are the two main types of variable scope?

Show solution

The two main types are global scope, where variables are available throughout the program, and local scope, where variables exist only within a function and disappear when the function exits.

  1. What is object scope in OOP, and what type of variable has object scope?

Show solution

Object scope refers to variables that are available to all methods of an object — these are called instance variables. They are prefixed with self.# (e.g. self.count) and belong to the object rather than just a single method.

  1. In a method, how can you tell the difference between a local variable and an instance variable?

Show solution

A local variable does not begin with self.# and only exists while the method runs. An instance variable does begin with self.# and is available to all methods of the object.

  1. Compare what happens to local and instance variables after a method exits.

Show solution

When a method exits:

  • Local variables disappear. They are deleted from memory and can no longer be accessed by any method of the object.

  • Instance variables (those prefixed with self.#) remain. They continue to exist as part of the object and can be accessed or modified by other methods of that object.

  1. Given this code, what will obj.count print after both increment calls?

class MyClass():
    def init(self):
        self.count = 0
    
    def increment(self):
        self.count = self.count + 1

obj = MyClass()
obj.increment()
obj.increment()

print(obj.count)
Show solution

It will print 2, because self.count starts at 0 and is increased by 1 each time increment() is called.

  1. Write a class called Counter that starts with a count of 10 and has a method addFive() that adds 5 to the count.

Show solution
class Counter:
    def __init__(self):
        self.count = 10
    def addFive(self):
        self.count += 5
  1. Why can two different LightSwitch objects have different values for switchIsOn at the same time?

Show solution

Because each LightSwitch object has its own separate instance variable self.switchIsOn. The state of one switch does not affect the state of another.

Last updated

Was this helpful?