2015年9月8日 星期二

[RR Python] Difference between built in repr() and str() - 兩者的不同

Official definition:
repr(object)
Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.


class str(object='')
Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
eval(expression[, globals[, locals]])
The arguments are a Unicode or Latin-1 encoded string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.

The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard __builtin__ module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:
>>
>>> x = 1
>>> print eval('x+1')
2
This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object instead of a string. If the code object has been compiled with 'exec' as the mode argument, eval()‘s return value will be None.
interesting demo below:
>>> s = "x +1"
>>> s
'x +1'
>>> print s
x +1
>>> str(s)
'x +1'
>>> print str(s)
x +1
>>> repr(s)
"'x +1'"

>>> print repr(s)
'x +1'
>>> x = 1
>>> eval(s)
2
>>> eval(str(s))
2
>>> eval(repr(s))
'x +1'
form Python Essential Reference, 4ed.
The __repr__() and __str__() methods create simple string representations of an object.The __repr__() method normally returns an expression string that can be evaluated to re-create the object.This is also the method responsible for creating the output of values you see when inspecting variables in the interactive interpreter.This method is invoked by the built-in repr() function.

a = [2,3,4,5]      # Create a list
s = repr(a)         # s = '[2, 3, 4, 5]'
b = eval(s)         # Turns s back into a list

Furthermore, Difference between __str__ and __repr__ in Python has extensively explanation of them. In short:

  1. __repr__ goal is to be unambiguous
  2. __str__ goal is to be readable
  3. Implement __repr__ for any class you implement. This should be second nature. Implement __str__ if you think it would be useful to have a string version which errs on the side of more readability in favor of more ambiguity.
  4. if you override __repr__, that's ALSO used for __str__, but not vice versa (Container’s __str__ uses contained objects’ __repr__)




沒有留言:

張貼留言