#๐ how `__repr__` is used
45 messages ยท Page 1 of 1 (latest)
@rustic prawn
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.
It's just a way to format the output when you are looking inside a object
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))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
Car(brand='Toyota', model='Corolla')
did you just copied from gpt?
I copied this from a old message here
init is constructor?
Yes
what if i dont write the repr like the constructor
What do you mean?
In general __repr__ is meant for unambigious details about the object where __str__ is for a human readable version.
The default implementation shows the class name and the memory address
why we need to make the object string
In order to print it
when we need it
When we are debugging
Mostly
it's just the standard way
normally it should show the type and some important values
both can return the same thing right if we give same code to both?
example please
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
ah ok
that makes sense
as we want to use print on class methods and attribute right?
is that wrong?
its for saying what the object is
in a human readable format
"__repr__"is mostly for debugging or devs, so it should show useful info abt the object.
"__str__" is more for cleaner human-readable output.
@lunar willow try to enclose your code on a ``
You can want to use print on an attribute, on the result of a method, inside a method, or even on the full object. When you do it on the full object
so like
x = MyClass()
print(x)
In that case, you'll see printed the thing that __repr__ returns
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__
yeah exactly, when you do:
Print (x)
Python will use __repr__ for the object's representation if it's defined.
No. Python will use __str__ if that's defined and will fall back to __repr__.
!e
class MyClass:
def __str__(self):
return 'This is __str__'
def __repr__(self):
return 'This is __repr__'
x = MyClass()
print(x)
print(repr(x))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | This is __str__
002 | This is __repr__
good catch, I mixed up "print()" behavior with the default object representation fallback
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.