214 Creating objects from a class
What does the term instantiation mean in object-oriented programming?
In the LightSwitch example, what does
oLightSwitch = LightSwitch()
do?
Explain the difference between a class and an object using the
LightSwitch
example.
Write a Python line of code that creates a third LightSwitch object called
oLightSwitch3
.
If
oLightSwitch1
andoLightSwitch2
are both instances of LightSwitch, what happens tooLightSwitch2.switchIsOn
when you calloLightSwitch1.turnOn()
?
Look at this code and predict what will be printed:
class LightSwitch():
def __init__(self):
self.switchIsOn = False
def turnOn(self):
# turn the switch on
self.switchIsOn = True
def turnOff(self):
# turn the switch off
self.switchIsOn = False
def show(self): # added for testing
print(self.switchIsOn)
# Main code
oLightSwitch1 = LightSwitch()
oLightSwitch2 = LightSwitch()
oLightSwitch1.turnOn()
oLightSwitch1.show()
oLightSwitch2.show()
Write a short code snippet that creates two
LightSwitch
objects, turns one on, and prints the state of both.
Last updated
Was this helpful?