Strange (?) list comprehension behavior

G

George Henry

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
 
A

Andrew Dalke

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
(e-mail address removed)
 
P

Peter Hansen

George said:
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...
 
P

Peter Hansen

Peter said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top