...continued
To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.
Syntax: the dot-notation
Example: simplified computer object
#oop #PythonProgramming #DotNotation
To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.
Syntax: the dot-notation
# Accessing an attribute
object.attribute
# Calling a method
object.method()
Example: simplified computer object
class Computer:
# __init__ is a special method (a constructor)
def __init__(self, brand, model):
# attributes
self.brand = brand
self.model = model
# methods
def powerOn(self):
print(f"Powering on {self.brand} {self.model}")
def powerOff(self):
print(f"Powering off {self.brand} {self.model}")
c1 = Computer("HP", "OMEN")
c2 = Computer("Dell", "XPS")
# Test code
print(c2.brand + " " + c2.model)
c1.powerOn()
# Output
Dell XPS
Powering on HP OMEN
#oop #PythonProgramming #DotNotation
❤1