Strange (?) list comprehension behavior

Discussion in 'Python' started by George Henry, Jul 19, 2003.

  1. George Henry

    George Henry Guest

    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.startswith(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

    >>>[op for op in PSILex.ops if "|| hello".startswith(op)]


    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'.

    >>>[op for op in PSILex.ops if op.startswith("|| hello")]


    yields "AttributeError: 'int' object has no attribute 'startswith',"
    however:

    >>>[type(op) for op in PSILex.ops]


    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.startswith(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
    http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
     
    George Henry, Jul 19, 2003
    #1
    1. Advertising

  2. George Henry

    Andrew Dalke Guest

    George Henry:
    > >>>[op for op in PSILex.ops if op.startswith("|| hello")]

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

    >
    > produces a list of <type 'str'> objects, as would be expected. So, what is
    > op? Is it a string, an int, or something else?


    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
     
    Andrew Dalke, Jul 19, 2003
    #2
    1. Advertising

  3. George Henry

    Peter Hansen Guest

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


    What are those ...'s ? You may have left out critical
    data for us.

    > ...
    > def __findAtom(self):
    > result = ""
    > ops = [op for op in PSILex.ops if self.text.startswith(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


    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...
     
    Peter Hansen, Jul 20, 2003
    #3
  4. George Henry

    Peter Hansen Guest

    Peter Hansen wrote:
    >
    > George Henry wrote:

    [a question]
    >
    > 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...


    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
     
    Peter Hansen, Jul 20, 2003
    #4
    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. Roland Puntaier

    strange list comprehension on generator

    Roland Puntaier, Aug 29, 2007, in forum: Python
    Replies:
    4
    Views:
    277
  2. Debajit Adhikary
    Replies:
    17
    Views:
    699
    Debajit Adhikary
    Oct 18, 2007
  3. Chris Mellon
    Replies:
    2
    Views:
    395
    Dustan
    Jan 18, 2008
  4. Vedran Furac(
    Replies:
    4
    Views:
    339
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
  5. Artur Siekielski
    Replies:
    6
    Views:
    294
    Terry Reedy
    May 7, 2010
Loading...

Share This Page