On Wed, Oct 3, 2012 at 1:38 AM, Steven D'Aprano <st...@pearwood.info> wrote:
<snip> > The long answer is a bit more subtle, and rather long. I had initial suspicions this would be the case Thanks for yours and Dave's detailed exposition! [...] > Python is no different: words, text if you will, that are part of the > code are written as normal: > > # source code > class Test: > pass > > x = Test # Test here refers to the variable Test, a class > > But to create a string object, you use quotation marks to tell Python > that this is data, not code, please create a string object: > > x = "Test" # Test here refers to a string, which is data > > Notice that the quotation marks are *delimiters*, they mark the start > and end of the string, but aren't part of the string in any way. Python > knows that the object is a string because you put it in string > delimiters, but the delimiters are not part of the string. I was not sure if the quotes were considered part of the string or not. Thanks for the clarification. > Now, take a step back and consider objects in general. There are two > things we might like to do to an arbitrary object: > > * display the object, which implicitly means turning it into a > string, or at least getting some representation of that object > as a string; > > * convert the object into a string. > > Python has two built-in functions for that: > > * repr, which takes any object and returns a string that represents > that object; > > * str, which tries to convert an object into a string, if that makes > sense. > > Often those will do the same thing. For example: > > py> str(42) == repr(42) == "42" > True > > But not always. For example: > > py> from decimal import Decimal as D > py> x = D("1.23") > py> print(str(x)) > 1.23 > py> print(repr(x)) > Decimal('1.23') These contrasting examples are very illuminating. So in print(str(x)) the object, D("1.23"), is being converted into a readable string, which makes the most sense as 1.23. But print(repr(x)) is giving a string representation of the object as code, which is more than just 1.23, the Decimal('1.23'). Am I understanding this correctly? > Unfortunately, the difference between str() and repr() is kind of > arbitrary and depends on the object. str() is supposed to return a > "human-readable" version of the object, for display, while repr() is > supposed to return a string which would work as code, but those are more > guidelines than hard rules. Will these fine distinctions be easy for me to pick up on as I progress in my Python studies? I suspect that I am going to have to experiment with str() and repr() in each new situation to see what results. > So we have two different ways of converting an object to a string. But > strings themselves are objects too. What happens there? > > py> s = "Hello world" # remember the quotes are delimiters, not part of the > string > py> print(str(s)) > Hello world > py> print(repr(s)) > 'Hello world' > > str() of a string is unchanged (and why shouldn't it be? it's already a > string, there's nothing to convert). > > But repr() of a string creates a new string showing the representation > of the original string, that is, what you would need to type in source > code to make that string. That means: > > 1) wrap the whole thing in delimiters (quotation marks) > 2) escaping special characters like tabs, newlines, and binary > characters. As to point 2), will repr() insert "\" (I am assuming Python uses a backslash like other languages to escape. I have not read about this in Python yet.) for these special characters? Will str() do the same? > Notice that the string returned by repr() includes quote marks as part > of the new string. Given the s above: > > py> t = repr(s) > py> print(t) > 'Hello world' > py> t > "'Hello world'" > > This tells us that the new string t includes single quote marks as the > first and last character, so when you print it, the single quote marks > are included in the output. But when you just display t interactively > (see below), the delimiters are shown. Another great example. I probably would have overlooked this. > Now, at the interactive interpreter, evaluating an object on its own > without saving the result anywhere displays the repr() to the screen. > Why repr()? Well, why not? The decision was somewhat arbitrary. So the designers of Python made this decision. I guess it had to be one way or the other. > print, on the other hand, displays the str() of the object directly to > the screen. For strings, that means the delimiters are not shown, > because they are not part of the string itself. Why str() rather than > repr()? Because that's what people mostly want, and if you want the > other, you can just say print(repr(obj)). So in the end it is a simple choice to give the users what they want and are already used to. > > Does this help, or are you more confused than ever? > This has been incredibly useful! Many thanks!! boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor