Using variable name of object in __repr__ method

Is there any way I can use an object’s variable name to be printed out in its __repr__ method? So, e.g. if I have an object of class TomeRater Good_Reads = TomeRater(), the output of __repr__ would contain something like “Good_Reads book rating database”

no, you can’t. Then you would need to use an instance variable which you set at instantiation:

TomeRater('Good reads')

the init method:

__init__(self, name):
   self.name = name

then you can use self.name in the __repr__ method

2 Likes