Python OOP Classes examples problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

23rd Mar 2017, 7:48 PM
acandas
acandas - avatar
1 Answer
+ 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.
23rd Mar 2017, 8:02 PM
Ryan