Behavior of python's repr method -
i understand goal of repr
unambiguous, behavior of repr
confused me.
repr('"1"') out[84]: '\'"1"\'' repr("'1'") out[85]: '"\'1\'"'
based on above code, think repr
put ''
around string. when try this:
repr('1') out[82]: "'1'" repr("1") out[83]: "'1'"
repr
put ""
around strings , repr("1")
, repr('1')
same.
why?
there 3 levels of quotes going on here!
the quotes inside string you're passing (only present in first example).
the quotes in string produced
repr
. keep in mindrepr
tries return string representation work python code, if pass string, add quotes around string.the quotes added python interpreter upon printing output. these confuses you. interpreter calling
repr
again, in order give idea of type of object being returned. otherwise, string1
, number1
identical.to rid of level of quoting, can see exact string produced
repr
, useprint(repr(...))
instead.
Comments
Post a Comment