This forum is now read-only. Please use our new forums at discuss.codecademy.com.

278 points
Picture
Submitted by
Ryan Iacovelli
almost 5 years ago

Can someone explain what __repr__() does a little more thoroughly please?

It's not clear from the practice program


20 votes

permalink

When we create new types by defining classes, we can take advantage of certain features of Python to make the new classes convenient to use. One of these features is "special methods", also referred to as "magic methods".

Special methods have names that begin and end with two underscores. We define them, but do not usually call them directly by name. Instead, they execute automatically under under specific circumstances.

It is convenient to be able to output the value of an instance of an object by using a print statement. When we do this, we would like the value to be represented in the output in some understandable unambiguous format. The __repr__ special method can be used to arrange for this to happen. If we define this method, it can get called automatically when we print the value of an instance of a class for which we defined this method. It should be mentioned, though, that there is also a __str__ special method, used for a similar, but not identical purpose, that may get precedence, if we have also defined it.

If we have not defined, the __repr__ method for the Point3D class, and have instantiated my_point as an instance of Point3D, and then we do this ...

print my_point

... we may see this as the output ...

<Point3D object at 0x7fc14c4848d0>

Not very nice, eh?

So, we define the __repr__ or __str__ special method, or both, to get better output.

Going beyond the instructions for Exercise 11: Building useful classes, a little, try this ...

class Point3D(object):
    def __init__(self,a,b,c):
        self.x = a
        self.y = b
        self.z = c
    def __repr__(self):
        return "Point3D(%d, %d, %d)" % (self.x, self.y, self.z)
    def __str__(self):
        return "(%d, %d, %d)" % (self.x, self.y, self.z)
my_point = Point3D(1, 2, 3)
print `my_point` # __repr__ gets called automatically
print my_point # __str__ gets called automatically

Output ...

Point3D(1, 2, 3)
(1, 2, 3)

17452 points
542ea68c9c4e9de17e00010c_599561501
Submitted by
Glenn Richard
almost 5 years ago

1 Comment

D2a02033914210021c5ab17ddb68bcb6?s=140&d=retro mikelane50 almost 5 years ago

thank you so much for all the useful explanations throughout this project. you have really enhanced my understanding of many of these concepts


1 vote

permalink

Hi, @nanduz,

The ` character is often referred to as a backquote. When an expression is enclosed in backquotes, __repr__ is called for the appropriate type to represent the resulting object, when you print it. If you print the value of an expression without backquotes, then __str__ will take precedence over __repr__ to represent the object as a str. If the type of the resultant object inherits from object, and __repr__ has not been overridden for that type, the object will be represented in a manner similar to this, when you use backquotes ...

<__builtin__.Point3D instance at 0x7f6511c883b0>

Note that the name of the class is part of the output, along with some hexadecimal gibberish. You can experiment further to find out what happens under various circumstances.

In any case, it is a good idea to write __repr__ and __str__ methods, when defining a class, so that instances of the class are represented in a more useful manner than the default.

17452 points
542ea68c9c4e9de17e00010c_599561501
Submitted by
Glenn Richard
over 4 years ago

1 Comment

7bd6fbad09b264138f95806d02356379?s=140&d=retro nanduz over 4 years ago

Thanks!


0 votes

permalink

The explanation above helped a lot to understand the str and repr .But could someone please explain the difference between the 2 print statements.

print `my_point` # __repr__ gets called automatically
print my_point # __str__ gets called automatically

Does `` overrides the precedence of str() over repr()?

62 points
7bd6fbad09b264138f95806d02356379?s=140&d=retro
Submitted by
nanduz
over 4 years ago