eval('000052') = 42?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Astan Chee

    #1

    eval('000052') = 42?

    Hi,
    I just tried to do
    eval('00052') and it returned 42.
    Is this a known bug in the eval function? Or have I missed the way eval
    function works?
    Thanks
  • Erik Max Francis

    #2
    Re: eval('000052') = 42?

    Astan Chee wrote:
    I just tried to do
    eval('00052') and it returned 42.
    Is this a known bug in the eval function? Or have I missed the way eval
    function works?
    String literals beginning with a 0 are in octal.

    Besides, using eval for such a narrow case is extremely unwise. Instead
    use int:
    >>int('00052' )
    52

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
    God grant me to contend with those that understand me.
    -- Thomas Fuller

    Comment

    • John Machin

      #3
      Re: eval('000052') = 42?

      On Feb 21, 3:09 pm, Astan Chee <s...@al.com.au wrote:
      Hi,
      I just tried to do
      eval('00052') and it returned 42.
      Is this a known bug in the eval function? Or have I missed the way eval
      function works?
      Thanks
      Eight fives are forty. Forty plus two is forty two. I see no bug here,
      only a language design strangeness which can be blamed on the then-
      pervasive influence of all things from Bell Labs :-)

      BTW, hasn't anyone told you not to play with the eval function? Put it
      down. You could catch something nasty.

      Comment

      • Laurent Pointal

        #4
        Re: eval('000052') = 42?

        Astan Chee a écrit :
        Hi,
        I just tried to do
        eval('00052') and it returned 42.
        Is this a known bug in the eval function? Or have I missed the way eval
        function works?
        Thanks
        Ad Erik replied, a literal value beginning by 0 is interpreted as an
        octal value (and beginning by 0x it is interpreted as hexadecimal value).

        You may use int construction from string, which allow to specify a base
        (default to ten):
        >>int('00052' )
        52
        >>int('00052',1 0)
        52
        >>int('00052',8 )
        42

        Note: this avoid possible evaluation of undesired Python expressions...

        A+

        Laurent.

        Comment

        • =?UTF-8?B?QW5kcsOp?= Malo

          #5
          Re: eval('000052') = 42?

          Astan Chee wrote:
          Hi,
          I just tried to do
          eval('00052') and it returned 42.
          Is this a known bug in the eval function? Or have I missed the way eval
          function works?
          You know, it's just the answer to the ultimate question of Life, the
          universe, and everything.

          SCNR, nd

          Comment

          • Bart Ogryczak

            #6
            Re: eval('000052') = 42?

            On Feb 21, 5:09 am, Astan Chee <s...@al.com.au wrote:
            Hi,
            I just tried to do
            eval('00052') and it returned 42.
            Is this a known bug in the eval function? Or have I missed the way eval
            function works?
            It works just fine. Read up on integer literals.
            >>52 #decimal
            52
            >>052 #octal
            42
            >>0x52 #hexadecimal
            82


            Comment

            • Neil Cerutti

              #7
              Re: eval('000052') = 42?

              On 2007-02-21, André Malo <auch-ich-m@g-kein-spam.comwrote:
              Astan Chee wrote:
              >
              >Hi,
              >I just tried to do
              >eval('00052' ) and it returned 42.
              >Is this a known bug in the eval function? Or have I missed the way eval
              >function works?
              >
              You know, it's just the answer to the ultimate question of
              Life, the universe, and everything.
              Yeah, but I was hoping the question would've turned out better.

              --
              Neil Cerutti

              Comment

              • Roger Miller

                #8
                Re: eval('000052') = 42?

                On Feb 20, 7:37 pm, "John Machin" <sjmac...@lexic on.netwrote:
                On Feb 21, 3:09 pm, Astan Chee <s...@al.com.au wrote:
                >
                Hi,
                I just tried to do
                eval('00052') and it returned 42.
                Is this a known bug in the eval function? Or have I missed the way eval
                function works?
                Thanks
                >
                Eight fives are forty. Forty plus two is forty two. I see no bug here,
                only a language design strangeness which can be blamed on the then-
                pervasive influence of all things from Bell Labs :-)
                So is this anachronism slated for removal in Python 3?


                Comment

                Working...