Class attribute name | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Class attribute name

When i write an attribute and later put = and the same name nothing happens, but when i change the self.attribute name but not the variable assigned and later create an object and print that specific attribute of the object, it prints out. But when i change the name of the variable assigned, it shows me an error. class Cat: def __init__(self, color, legs): self.color = color self.legs = legs felix = Cat("ginger", 4) print(felix.color) it works, later when i change color to colotr it works: class Cat: def __init__(self, color, legs): self.colotr = color self.legs = legs felix = Cat("ginger", 4) print(felix.colotr) but when i change the name it says name "colotr is not defined": class Cat: def __init__(self, color, legs): self.color = colotr self.legs = legs felix = Cat("ginger", 4) print(felix.color)

24th Apr 2020, 6:34 PM
Ismael Browne
Ismael Browne - avatar
2 Answers
0
That is because you are going to use 'colotr', but the argument is called 'color'. Change name of argument or usage.
24th Apr 2020, 6:54 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
0
thanks
24th Apr 2020, 7:01 PM
Ismael Browne
Ismael Browne - avatar