216 Calling methods
In this section, you will learn how to create objects from a class and call their methods, seeing how each object maintains its own independent data and behaviour.
<object> = <ClassName>(<optional arguments>)Calling Methods of An Object
<object>.<methodName>(<any arguments>)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
oLightSwitch = LightSwitch() # create a LightSwitch object
# Calls to methods
oLightSwitch.show()
oLightSwitch.turnOn()
oLightSwitch.show()
oLightSwitch.turnOff()
oLightSwitch.show()
oLightSwitch.turnOn()
oLightSwitch.show()Creating Multiple Instances from the Same Class
Review questions
Last updated
Was this helpful?