Python Forum
question about __repr__ in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about __repr__ in a class
#1
hi
the below code is in:https://realpython.com/python-mutable-vs...om-classes

class Point: def __init__(self, x, y): self._x = x self._y = y @property def x(self): return self._x @property def y(self): return self._y def __repr__(self): return f"{type(self).__name__}(x={self.x}, y={self.y})"
what does the last line do also what does the __repe__ method do? how can i use the __repr__ method? if it is omitted, does it cause any problems?
thanks
Reply
#2
Objects have two string representations: __str__ is the pretty one, and __repr__ is the smart one. __str__ is used when you call str() or print() an object. __repr__ is used when you call repr() or when you print an object as part of a collection (list, dictionary, set, tuple).

Have you tried omitting the method to see what happens? What do you think will happen?
Reply
#3
Don't forget to use the search field in the Python documentation
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
(Jan-11-2024, 12:14 PM)deanhystad Wrote: Objects have two string representations: __str__ is the pretty one, and __repr__ is the smart one. __str__ is used when you call str() or print() an object. __repr__ is used when you call repr() or when you print an object as part of a collection (list, dictionary, set, tuple).

Have you tried omitting the method to see what happens? What do you think will happen?

hi
i omitted it, but i did not see any change. what can i write or add to the above code too see the changes occure with ommitting the __repr__ method?
thanks again for reply
Reply
#5
If the method __str__ is not available, then __repr__ is called instead.

If you just want a class with some variables and a nice representation, you could use dataclasses instead:
import math from dataclasses import dataclass @dataclass class PointD: x: int y: int @property def alpha(self): """ alpha for polar coordinates """ return math.degrees(math.atan2(self.y, self.x)) @property def hypot(self): """ Length of hypot """ return math.hypot(self.x, self.y) @property def polar(self): """ Poloar coordinates """ return self.alpha, self.hypot @classmethod def from_ploar(cls, polar): """ Create a point from ploar coordinates polar := (alpha, hypot) """ alpha = math.radians(polar[0]) return cls(math.cos(alpha) * polar[1], math.sin(alpha) * polar[1]) p2 = PointD(7, 9) print(p2) print(f"{p2!s}") print(f"{p2!r}") print(f"{p2.alpha}") print(f"{p2.hypot}") print(f"{p2.polar}") print(PointD.from_ploar(p2.polar))
Output:
PointD(x=7, y=9) PointD(x=7, y=9) PointD(x=7, y=9) 52.1250163489018 11.40175425099138 (52.1250163489018, 11.40175425099138) PointD(x=6.999999999999998, y=9.0)
akbarza likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 4,131 May-02-2023, 09:09 PM
Last Post: billykid999
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 2,429 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  newbie question....importing a created class ridgerunnersjw 5 4,417 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Class question robdineen 7 5,413 May-30-2020, 05:44 AM
Last Post: Calli
  TypeError: __repr__ returned non-string (type dict) shockwave 0 4,335 May-17-2020, 05:56 PM
Last Post: shockwave
  Question about naming variables in class methods sShadowSerpent 1 3,277 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  class question jrauden 1 2,698 Feb-16-2020, 10:14 AM
Last Post: ibreeden
  Class Question esteel 5 5,886 May-22-2018, 10:27 AM
Last Post: esteel
  First Post/ Class Error Question jvan1601 2 4,766 May-01-2017, 04:21 AM
Last Post: jvan1601

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.