Python - Constructors and Object Initialization
What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Buddy")
print(my_dog.name)What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Buddy")
print(my_dog.name)__init__ method sets self.name to "Buddy" when my_dog is created.my_dog.name outputs the string "Buddy" stored in the attribute.__init__ [OK]15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions