Module 5: Introduction to Object-Oriented Programming (OOP)
Using Objects: Instantiation and Calling Methods
-
Instantiation (Creating an Object): You create an object by calling the class name as if it were a function, passing any arguments required by the
__init__
method (excludingself
).my_dog = Dog("Buddy", 3) # Creates an instance of the Dog class
-
Accessing Attributes: Use the dot notation:
object_name.attribute_name
.print(my_dog.name) # Output: Buddy
-
Calling Methods: Use the dot notation:
object_name.method_name()
.my_dog.bark() # Output: Buddy says Woof!