>>> class foo: ... def __str__(self): ... return "bar" ... >>> f = foo() >>> f <__main__.foo instance at 0x1004d2638> >>> str(f) 'bar' >>> repr(f) '<__main__.foo instance at 0x1004d2638>' >>> class foo overrides the base implementation of __str__; hence, the result is the output above when "casting"/converting to str (not a python junky, so Chris can fill you in on the proper terminology). As a result, as Chris pointed out, if you did something exceptional in __str__ (like dividing by 0), you would not even have the above output when getting the readable string via str(). On 6/7/11, Homme, James <james.homme@xxxxxxxxxxxx> wrote: > Hi Chris, > This is my understanding of what you said. If you really want to see what > something looks like, use %r. If you want to see how it is represented by > some object, use %s. Is that anywhere close? > > Thanks. > > Jim > > -----Original Message----- > From: programmingblind-bounce@xxxxxxxxxxxxx > [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Q > Sent: Monday, June 06, 2011 11:51 PM > To: programmingblind@xxxxxxxxxxxxx > Subject: Re: Python Question About Format Specifiers > > %r vs. %s... > Use cases: > Use standard format characters (%s, %d, %.2f) when formatting output you > can trust. > Use %r when you need to output something you cannot trust, for instance > if you're logging a failed condition and don't actually know current > state of some object. For instance: > > try: > do_something(some_val) > except SomeStrangeError: > logging.exception("Failed to do something. Value of some_val was %r" > % some_val > > Because an exception was thrown, and at this point we don't know why, we > want to communicate the state of the program as well as we can to the > log. This means printing out the raw state of the referenced some_val > using %r. Then we don't have to worry about the format characters > potentially munging our some_val into a different format for > presentation and potentially hiding something. > Also, since __str__, the method which is called when you go > str(some_obj), which is what's going on under the covers when you use %s > in your statements, might have side effects. Consider the case of an > object which is initialized with a name, and whose __str__ method prints > a representation of the object which includes that name. > Then, imagine you've got code logging with that object, instantiate one, > forget to give it a name, so the __str__ method raises an exception. So > if then you try to log an exception with a string representation of the > object, that also raises an exception and your log doesn't get written. > This would obviously be poor design, as an object should complain about > not getting a name by default, but it is for illustrative purposes. > I hope this demystifies things a bit, > Q > > > On 6/6/2011 3:52 PM, Homme, James wrote: >> Hi Dave, >> I read in the Python Standard Library documentation about %r and %s, and >> one said that it uses repr, and the other said that it uses str. Next >> time, I'll follow the documentation path the whole way and actually go and >> read about the functions. Thanks for being so nice about answering this. >> >> Jim >> >> -----Original Message----- >> From: programmingblind-bounce@xxxxxxxxxxxxx >> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of David Tseng >> Sent: Monday, June 06, 2011 3:36 PM >> To: programmingblind@xxxxxxxxxxxxx >> Subject: Re: Python Question About Format Specifiers >> >> http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python >> >> In other words, it's the difference between repr() and str(). >> >> On 6/6/11, Homme, James<james.homme@xxxxxxxxxxxx> wrote: >>> Hi, >>> My goal in this program is to properly explain above lines what they are >>> doing. I'm unsure what goes on with the last line. I see what it's doing >>> when I run the program, but I don't know why it's doing it. >>> >>> # Get some input from the user and work with it. >>> # >>> # Print and use a comma to make the input go to the same line as the >>> question. >>> print "How old are you?", >>> # Get input and put it into age. >>> age = raw_input() >>> # Now, give the prompt and get the input all at once. >>> # Note that now we have to put a space at the end of the quoted string. >>> height = raw_input("How tall are you? ") >>> # Now do it again. >>> weight = raw_input("How much do you weigh? ") >>> # Now print out the variables with raw string format specifiers. >>> # This turns them into single quoted strings properly formatted. >>> print "So, you're %r old, %r tall and %r heavy." % ( age, height, weight) >>> # I'm unsure how changing this to %s does what it does. >>> print "So number 2, you're %s old, %s tall and %s heavy." % ( age, >>> height, >>> weight) >>> >>> Thanks. >>> >>> Jim >>> Jim Homme, >>> Usability Services, >>> Phone: 412-544-1810. >>> >>> >>> ________________________________ >>> This e-mail and any attachments to it are confidential and are intended >>> solely for use of the individual or entity to whom they are addressed. If >>> you have received this e-mail in error, please notify the sender >>> immediately >>> and then delete it. If you are not the intended recipient, you must not >>> keep, use, disclose, copy or distribute this e-mail without the author's >>> prior permission. The views expressed in this e-mail message do not >>> necessarily represent the views of Highmark Inc., its subsidiaries, or >>> affiliates. >>> >> __________ >> View the list's information and change your settings at >> //www.freelists.org/list/programmingblind >> >> __________ >> View the list's information and change your settings at >> //www.freelists.org/list/programmingblind >> > > __________ > View the list's information and change your settings at > //www.freelists.org/list/programmingblind > > __________ > View the list's information and change your settings at > //www.freelists.org/list/programmingblind > > __________ View the list's information and change your settings at //www.freelists.org/list/programmingblind