Re: list Integer indexing dies??

Discussion in 'Python' started by Ishwor, Dec 23, 2004.

  1. Ishwor

    Ishwor Guest

    On Thu, 23 Dec 2004 13:33:16 -0300, Batista, Facundo
    <> wrote:
    >
    >
    > [Ishwor]
    >
    > #- > What should 035[0] cough up? Be carefull it should
    > #-
    > #- >>>035[0]
    > #- 3 # my own opinion.

    why 3? The reason we get 3 and not 0 here is the *fact* that Python
    knows that its an octal rep. and not decimal ;-)
    035[2] could return error here. Same for hex. No idea for binary. ~;-(

    > #-
    > #- > cough up the same as 29[0].
    > #-
    > #- >>>29[0]
    > #- 2 #again my own opinion

    since it's decimal its fine to get 2 which is at offset 0.

    [snip]

    --
    cheers,
    Ishwor Gurung
     
    Ishwor, Dec 23, 2004
    #1
    1. Advertising

  2. Ishwor

    Jeff Shannon Guest

    Ishwor wrote:

    >On Thu, 23 Dec 2004 13:33:16 -0300, Batista, Facundo
    ><> wrote:
    >
    >
    >>[Ishwor]
    >>
    >>#- > What should 035[0] cough up? Be carefull it should
    >>#-
    >>#- >>>035[0]
    >>#- 3 # my own opinion.
    >>
    >>

    >why 3? The reason we get 3 and not 0 here is the *fact* that Python
    >knows that its an octal rep. and not decimal ;-)
    >
    >


    Except that, at the point where the indexing takes place, Python
    *doesn't* know that it's an octal rep. All integer literals, regardless
    of representation, generate exactly the same bytecode.

    >>> def f():

    .... x = 035
    .... y = 29
    ....
    >>> dis.dis(f)

    0 SET_LINENO 1

    3 SET_LINENO 2
    6 LOAD_CONST 1 (29)
    9 STORE_FAST 1 (x)

    12 SET_LINENO 3
    15 LOAD_CONST 1 (29)
    18 STORE_FAST 0 (y)
    21 LOAD_CONST 0 (None)
    24 RETURN_VALUE
    >>>


    Given that Python may not even have access to the .py file, only the
    ..pyc (which has lost all record of the source representation), there's
    no way for the interpreter to do what you suggest.

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Dec 23, 2004
    #2
    1. Advertising

  3. Ishwor

    Dan Bishop Guest

    Jeff Shannon wrote:
    > Ishwor wrote:
    >
    > >On Thu, 23 Dec 2004 13:33:16 -0300, Batista, Facundo
    > ><> wrote:
    > >
    > >
    > >>[Ishwor]
    > >>
    > >>#- > What should 035[0] cough up? Be carefull it should
    > >>#-
    > >>#- >>>035[0]
    > >>#- 3 # my own opinion.
    > >>
    > >>

    > >why 3? The reason we get 3 and not 0 here is the *fact* that Python
    > >knows that its an octal rep. and not decimal ;-)
    > >
    > >

    >
    > Except that, at the point where the indexing takes place, Python
    > *doesn't* know that it's an octal rep. All integer literals,

    regardless
    > of representation, generate exactly the same bytecode.
    >
    > >>> def f():

    > ... x = 035
    > ... y = 29
    > ...
    > >>> dis.dis(f)

    > 0 SET_LINENO 1
    >
    > 3 SET_LINENO 2
    > 6 LOAD_CONST 1 (29)
    > 9 STORE_FAST 1 (x)
    >
    > 12 SET_LINENO 3
    > 15 LOAD_CONST 1 (29)
    > 18 STORE_FAST 0 (y)
    > 21 LOAD_CONST 0 (None)
    > 24 RETURN_VALUE
    > >>>

    >
    > Given that Python may not even have access to the .py file, only the
    > .pyc (which has lost all record of the source representation),

    there's
    > no way for the interpreter to do what you suggest.

    And even if there was, what should (104 + 0x68 + 0150)[0] print?
     
    Dan Bishop, Dec 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C
    Replies:
    0
    Views:
    527
  2. Ishwor

    list Integer indexing dies??

    Ishwor, Dec 23, 2004, in forum: Python
    Replies:
    10
    Views:
    514
    Mike Meyer
    Dec 24, 2004
  3. Ishwor
    Replies:
    1
    Views:
    356
    Serhiy Storchaka1745620946
    Dec 23, 2004
  4. Emin
    Replies:
    4
    Views:
    434
    Paul McGuire
    Jan 12, 2007
  5. Christopher M. Lusardi
    Replies:
    3
    Views:
    217
    Alan J. Flavell
    Oct 19, 2003
Loading...

Share This Page