Unique Object
Code
Methods inside the class:
say_meow;set_namewith thenameargument .
set_name defines an additional attribute name and sets the value of the given statement.
self means that this variable is the class attribute, not the local variable.
Object cat is created.
We call say_meow method and change the attribute of the class by referring to cat.hair_color.
We set 'Princess' as the name using set_name method.
12345678910111213class Cat: def say_meow(self): print('Meow') def set_name(self, name): self.name = name print(self.hair_color, 'cat name is', self.name) # Instantiating an object cat = Cat() cat.say_meow() # Creating the unique сat cat.hair_color = 'White' cat.set_name('Princess')
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Stel mij vragen over dit onderwerp
Vat dit hoofdstuk samen
Toon voorbeelden uit de praktijk
Awesome!
Completion rate improved to 7.69
Unique Object
Veeg om het menu te tonen
Code
Methods inside the class:
say_meow;set_namewith thenameargument .
set_name defines an additional attribute name and sets the value of the given statement.
self means that this variable is the class attribute, not the local variable.
Object cat is created.
We call say_meow method and change the attribute of the class by referring to cat.hair_color.
We set 'Princess' as the name using set_name method.
12345678910111213class Cat: def say_meow(self): print('Meow') def set_name(self, name): self.name = name print(self.hair_color, 'cat name is', self.name) # Instantiating an object cat = Cat() cat.say_meow() # Creating the unique сat cat.hair_color = 'White' cat.set_name('Princess')
Bedankt voor je feedback!