#๐Ÿ”’ how `__repr__` is used

45 messages ยท Page 1 of 1 (latest)

rustic prawn
#

please help me understand this

versed valveBOT
#

@rustic prawn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

misty linden
rustic prawn
#

example please

misty linden
# rustic prawn example please

!e

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def __repr__(self):
        # Returns a string that looks like the constructor call
        return f"Car(brand='{self.brand}', model='{self.model}')"

my_car = Car("Toyota", "Corolla")
print(repr(my_car))
versed valveBOT
rustic prawn
#

did you just copied from gpt?

misty linden
#

I copied this from a old message here

rustic prawn
#

init is constructor?

misty linden
#

Yes

rustic prawn
#

what if i dont write the repr like the constructor

misty linden
#

What do you mean?

graceful heron
#

In general __repr__ is meant for unambigious details about the object where __str__ is for a human readable version.

graceful heron
rustic prawn
#

why we need to make the object string

graceful heron
#

In order to print it

rustic prawn
#

when we need it

misty linden
#

When we are debugging

rustic prawn
#

just for debugging?

#

on what it helps?

misty linden
#

Mostly

misty linden
#

normally it should show the type and some important values

rustic prawn
#

both can return the same thing right if we give same code to both?

rustic prawn
clever shard
#

When you debug, you want to add some prints here and there to see what is the value of a variable. print calls __repr__ to get the representation of the thing you print.

#

So you need __repr__ to return a string with all the context and detail you want, in order to debug correctly

rustic prawn
#

ah ok

#

that makes sense

#

as we want to use print on class methods and attribute right?

#

is that wrong?

fallen turret
#

in a human readable format

lunar willow
#
"__repr__"is mostly for debugging or devs, so it should show useful info abt the object.
#
"__str__" is more for cleaner human-readable output.
misty linden
#

@lunar willow try to enclose your code on a ``

clever shard
#

So here, if MyClass does not have a repr, you'll see something like

Myclass object at x891716291917166

But if it does, you'll see the thing returned by __repr__

lunar willow
elder monolith
#

!e

class MyClass:
    def __str__(self):
        return 'This is __str__'
    def __repr__(self):
        return 'This is __repr__'

x = MyClass()
print(x)
print(repr(x))
versed valveBOT
lunar willow
#

good catch, I mixed up "print()" behavior with the default object representation fallback

versed valveBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.