class Circle: pi = 3.14 def __init__(self, diameter): self.radius = diameter / 2 def area(self): return self.pi * self.radius ** 2 def circumference(self): return self.pi * 2 * self.radius def __repr__(self): return 'Circle with radius' + ' ' + str(self.radius) medium_pizza = Circle(12) teaching_table = Circle(36) round_room = Circle(11460) print(medium_pizza) print(teaching_table) print(round_room)
This is an exercise I have done that teaches string representation. My question is more to understand the use of this method ( and to understand it better).
Why does python have this dunder to get the object representation. Couldn’t the coding be made simpler if a print(object) just printed the stored value anyway, reducing a dunder.
If a coder needed to see the location and class data of the object he/she could call it explicitly with another command?? As a coder don’t we need the stored value more often than the default representation.
Do you reckon this will be improved in next python update?
If my questions are not that valid than how does default representation of an object valuable/ worthy in the coding or later stages?
All of this coz I was thinking its bit redundant having to call a repr dunder to get the value when the an update can easily make it straightforward. Please let me know if the community feels the same.
When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer!