0
Python OOP Classes examples problem
There is an example in Python Courses, Object Oriented Programming Classes section: class Cat: def __init__(self, color, legs): self.color = color self.legs = legs felix = Cat("ginger", 4) print(felix.color) print(felix.legs) Don't you think this changes should be done: class Cat: def __init__(self, color, legs): self.whichcolor = color self.howmanylegs = legs felix = Cat("ginger", 4) print(felix.whichcolor) print(felix.howmanylegs) So, colors and legs don't mix up
1 Réponse
+ 1
I think you are mixing up the variables and getter/setter functions that you may have seen in other programming languages. The typical coding convention is that variables are nouns (color) while functions start with a verb (whichcolor). In other languages like C++, member variables are often private, so you would have to put felix.whichcolor() in order to get the color of felix. However, in Python all fields are public, making this unnecessary. Instead, felix.color is the color of felix, not a function that returns the color. I hope this answered your question.