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!

  1. the quotes inside string you're passing (only present in first example).

  2. the quotes in string produced repr. keep in mind repr tries return string representation work python code, if pass string, add quotes around string.

  3. 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, string 1 , number 1 identical.

    to rid of level of quoting, can see exact string produced repr, use print(repr(...)) instead.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -