[Tutor] unstring

Dave Angel davea at davea.name
Wed Jun 19 04:02:23 CEST 2013


On 06/18/2013 09:41 PM, Jim Mooney wrote:
> Is there a way to unstring something? That is str(object) will give me
> a string, but what if I want the original object back, for some
> purpose, without a lot of foofaraw?
>

In general, definitely not.  A class can define just about anything in 
its __str__() method, though it'd be polite for it to return a str.  But 
there's no promise that you can do anything in particular to the str to 
even recover the type of the original object, never mind the value.

For example, in Python 3.3

 >>> class Bar:
...     def __str__(self):
...         return "42"
...
 >>> x = Bar()
 >>> str(x)
'42'
 >>> str(42)
'42'

-- 
DaveA


More information about the Tutor mailing list