Strange (?) list comprehension behavior

Andrew Dalke adalke at mindspring.com
Sat Jul 19 18:53:17 EDT 2003


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
                    dalke at dalkescientific.com






More information about the Python-list mailing list