Does the __repr()__ method need to provide all the attributes of an object?

Question

When implementing the __repr__() method, do all object attributes have to be used when constructing the string?

Answer

While you are allowed to include or exclude any information from the object that you want, the Python documentation recommends that the implementation for __repr__() should contain as much information as possible and if, at all possible, it should contain whatever is necessary to recreate the object. The __str__() method also returns a string representing the object but it can be used for a more informal representation of the object.

5 Likes

In a sense, instance.__repr__() is a getter, only it gets everything, and the expected return value will be a string representation of the data points of the instance as initialized and or modified.

The operative phrase above was return value. Getters don’t do things; they return things.

class Foo(object):
  def __init__(self, foo):
    self.foo = foo
  def __repr__(self):
    return "foo: {}".format(self.foo)

foo = Foo('foo')
print (foo)
>>> 
=================== RESTART: D:/cc/discuss/FAQ/__repr__.py ===================
foo: foo
>>> 
17 Likes

com
may anyone explain this line for me i mean why should i write that line , isn’t the variable " name " already defined in the function parameter ?

2 Likes

A function’s parameters are names that are local to that function. When the function completes its execution, the local variables are lost. Therefore, if we wish to preserve the values of the function parameters, we need to save those values somewhere, for example, in instance variables, which are not merely local to the function.

An instance variable does not necessarily need to have the same name as a parameter that is used to set its value.

16 Likes

so it’s necessary to save every method’s parameter in an instance variable ?

We don’t necessarily always need to save the value of a parameter exactly as it was given.

See the following:

class Circle:
  pi = 3.14149265
  def __init__(self, circumference):
    self.radius = circumference / Circle.pi
  def get_circumference(self):
    return self.radius * Circle.pi
  def get_radius(self):
    return self.radius

c = Circle(10.0)
print(c.get_circumference())
print(c.get_radius())

Output:

10.0
3.183200189884258

We instantiated the Circle with the circumference, but calculated its radius and saved that value. In a sense, we discarded the value provided by the argument, but we can use the saved radius to recover an approximation of the circumference. Admittedly, we could lose some precision in the process.

The guiding principle is that we use parameters of the __init__ method to provide information that is needed to instantiate an instance. We might even use some of that information to perform other tasks related to the context in which the instance was created, such as maintaining files, or coordination with other objects. However, in some cases, we might not need to retrieve the provided information later, exactly as it was given originally.

15 Likes

Really thank you so much for making that clear !!

2 Likes

Another example:

class AdoptedCat:
  def __init__(self, name, is_hungry):
    self.name = name
    if is_hungry:
      print("Feed {} now!".format(self.name))
    else:
      print("Feed {} later".format(self.name))

cat = AdoptedCat("Monty", True)

Output:

Feed Monty now!

We saved name, but not is_hungry. However, we used both.

10 Likes

why did you put True as the second parameter?

cat = AdoptedCat(“Monty”, True)

is_hungry is a variable that can store Bool value. It can either be True or False. “is_hungry” is a descriptive term used to help communicate that a Bool value goes there.

little note: isn’t the circumference of a circle 2 * PIE * R?
I think you missed the 2 in your calculations.