Strange (?) list comprehension behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Henry

    Strange (?) list comprehension behavior

    I am a Python newbie, using IDLE, writing a lexical analyzer for a small
    expression language in Python 2.2.3.

    class PSILex:
    ops = ["~", "!", "@", ... "|", "||", ... ]
    ...
    def __findAtom(self ):
    result = ""
    ops = [op for op in PSILex.ops if self.text.start swith(op)]

    .... results in a traceback and "TypeError: expected a character buffer
    object." In trying to figure out what was going on, in the Python Shell I
    subsequently tried
    [color=blue][color=green][color=darkred]
    >>>[op for op in PSILex.ops if "|| hello".startswi th(op)][/color][/color][/color]

    with the same result. Printing type(self.text) immediately prior ot the
    above line proves that that attribute's type has not been compromised, so
    the problem appears to be with 'op'.
    [color=blue][color=green][color=darkred]
    >>>[op for op in PSILex.ops if op.startswith(" || hello")][/color][/color][/color]

    yields "AttributeError : 'int' object has no attribute 'startswith',"
    however:
    [color=blue][color=green][color=darkred]
    >>>[type(op) for op in PSILex.ops][/color][/color][/color]

    produces a list of <type 'str'> objects, as would be expected. So, what is
    op? Is it a string, an int, or something else? It appears that the
    interpreter may be confused. I persuaded the interpreter to cooperate by
    injecting an explicit evaluation of type(op):

    ops = [op for op in PSILex.ops if type(op) == type("") and \
    self.text.start swith(op)]

    and this does what I want, and what I would expect without 'type(op) ==
    type("") and '.

    Can someone explain what is happening here? Or should I send my code to
    ww.python.org as a "defect report?"

    Regards,
    George

    _______________ _______________ _______________ _______________ _____
    Protect your PC - get McAfee.com VirusScan Online



  • Andrew Dalke

    #2
    Re: Strange (?) list comprehension behavior

    George Henry:[color=blue][color=green][color=darkred]
    > >>>[op for op in PSILex.ops if op.startswith(" || hello")][/color][/color]
    >
    > yields "AttributeError : 'int' object has no attribute 'startswith',"
    > however:
    >[color=green][color=darkred]
    > >>>[type(op) for op in PSILex.ops][/color][/color]
    >
    > produces a list of <type 'str'> objects, as would be expected. So, what is
    > op? Is it a string, an int, or something else?[/color]

    Can you copy&paste what you did, rather than interpret the output?
    There's no way to get both of these errors that I know, and I expect
    it to be more likely that you didn't see a <type 'int'> somewhere in
    the output rather than a bug in Python.

    You can also try

    [type(op) for op in PSILex.ops if type(op) != type("")]

    Andrew
    dalke@dalkescie ntific.com


    Comment

    • Peter Hansen

      #3
      Re: Strange (?) list comprehension behavior

      George Henry wrote:[color=blue]
      >
      > class PSILex:
      > ops = ["~", "!", "@", ... "|", "||", ... ][/color]

      What are those ...'s ? You may have left out critical
      data for us.
      [color=blue]
      > ...
      > def __findAtom(self ):
      > result = ""
      > ops = [op for op in PSILex.ops if self.text.start swith(op)]
      >
      > ... results in a traceback and "TypeError: expected a character buffer
      > object." In trying to figure out what was going on, in the Python Shell I
      > subsequently tried[/color]

      Please post the precise traceback, cut and pasted. If it really said
      that, and your subsequent analysis is valid, I think one of your "ops"
      is not a string...

      Comment

      • Peter Hansen

        #4
        Re: Strange (?) list comprehension behavior

        Peter Hansen wrote:[color=blue]
        >
        > George Henry wrote:[/color]
        [a question][color=blue]
        >
        > Please post the precise traceback, cut and pasted. If it really said
        > that, and your subsequent analysis is valid, I think one of your "ops"
        > is not a string...[/color]

        Cancel that... I didn't see your subsequent followup before I posted,
        because it was not linked to the previous thread. You already found
        the problem... :-)

        -Peter

        Comment

        Working...