[Tutor] __str__ Vs __repr__

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Nov 30 12:58:43 EST 2021


On Tue, 30 Nov 2021 22:35:14 +0530, Manprit Singh
<manpritsinghece at gmail.com> declaimed the following:

>After assigning a value to a variable, Lets say variable is a, and the
>value assigned to it is 5,
>On an interactive shell after writing a and then hitting Enter key, it
>prints the value of a, that is 5, This happens due to __str__ or __repr__?
>

	Using that same shell, you can easily determine which is used. Note
that __str__() formats "human readable" text, while __repr__() formats
"program" text.

PythonWin 3.8.2 (default, Aug 25 2020, 15:54:26) [MSC v.1900 64 bit
(AMD64)] on win32.
Portions Copyright 1994-2018 Mark Hammond - see 'Help/About PythonWin' for
further copyright information.
>>> a = 5
>>> a
5
>>> b = "5"
>>> b
'5'
>>> print(a, b)
5 5
>>> print(str(a), str(b))
5 5
>>> print (repr(a), repr(b))
5 '5'
>>> 

	Note how the sell displays b with quote marks -- which is what you get
when asking for repr() of a string object.

>What should I use(__str__ or  __repr__) if I want something to be printed
>when I print an object of a user defined class ?

	You would have to write a __str__() method to explicitly format some
form of the object for user output, or __repr__() for some form that might
be used in the code as a literal. Note that, for many classes, there is no
reasonable __repr__() that generates a literal that could be used in the
program -- hence why one often gets just text stating "class" (or "type")
instance "ID" (for common Python, memory address).

>>> class C():
... 	def __init__(self, arg):
... 		self.arg = arg
... 	def __repr__(self):
... 		return "Class C instance with arg = %r" % self.arg
... 	
>>> c1 = C(5)
>>> c2 = C("5")
>>> c1
Class C instance with arg = 5
>>> c2
Class C instance with arg = '5'
>>> 

	%r invokes repr() which invokes the object __repr__()
	%s invokes str() which invokes __str__() IF IT EXISTS, otherwise it
uses __repr__()

>>> str(c1)
'Class C instance with arg = 5'
>>> str(c2)
"Class C instance with arg = '5'"

	Note how str(c2) shows the quoted '5' which matches the repr() of the
object. Now, add a __str__() method using %s translation

>>> class D():
... 	def __init__(self, arg):
... 		self.arg = arg
... 	def __repr__(self):
... 		return "Class D instance with arg = %r" % self.arg
... 	def __str__(self):
... 		return "Class D instance with arg = %s" % self.arg
... 	
>>> d1 = D(5)
>>> d2 = D("6")
>>> d1
Class D instance with arg = 5
>>> d2
Class D instance with arg = '6'
>>> str(d1)
'Class D instance with arg = 5'
>>> str(d2)
'Class D instance with arg = 6'
>>> repr(d1)
'Class D instance with arg = 5'
>>> repr(d2)
"Class D instance with arg = '6'"
>>> 

	The shell still uses the repr() format, but str() uses the other
format, which doesn't add quotes.

>>> print(d1, d2)
Class D instance with arg = 5 Class D instance with arg = 6
>>> print(c1, c2)
Class C instance with arg = 5 Class C instance with arg = '5'
>>> 

	print() uses the __str__() form where possible, otherwise devolves to
__repr__() form.
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list