[albatross-users] Bug with persistentlists and such (1.10)
Michael C. Neel
neel at mediapulse.com
Wed Nov 12 06:29:41 EST 2003
> | Doesn't map imply a function call for every element of the list?
>
> If it is has a list interface, then yes. But if it has a list
> interface, we want to do that anyhow. Look at what the code does:
>
> try:
> if str(value_attr) in map(str, value):
> ctx.write_content(' checked')
> [handle exception]
>
> Compare this with the original code:
>
> if type(value) in (type([]), type(())):
> if str(value_attr) in map(str, value):
> ctx.write_content(' checked')
>
> As you can see, it still does the same thing, and map will fail on
> objects which doesn't have a list interface. If they _do_ have a list
> interface, it's desirable that expr does work as expected. If it
> looks like a list, walks like a list and talks like a list, then it is
> a list.
But strings *do* have a list interface. Consider:
>>> test = "testme"
>>> test[2]
's'
>>> map(str, test)
['t', 'e', 's', 't', 'm', 'e']
So while the odds are low that when value is a string it will match, we
still ran a function call for every item (in this case the letters).
> | A bit high on the overhead don't you think for a type check?
>
> Doing an interface check instead of type check? And doing it in C
> instead of Python (map is C)? No, that's lower overhead, not higher.
>
Skipping the string error for a second, in this case we are going to map
anyway so there wouldn't be harm - but if you are type checking with map
and don't need the output of map, like you just want to get a len(),
then we just called str() on every element of the list. It's this
function call I mean when I say overhead, not map itself.
> | I'm not sure how it checks now, but a test of "isinstance(val,
> | list)" will catch lists and classes built from list.
>
> What do you mean by <classes built from list>? List is a native type,
> it's not a class, so you can't inherit from it.
>
Welcome to python 2.3, it's not your father's python:
>>> class MyList(list):
... pass
...
>>> lst = MyList()
>>> lst
[]
>>> lst.append(2)
>>> lst.append(3)
>>> lst.append(4)
>>> lst
[2, 3, 4]
>>> type(lst)
<class '__main__.MyList'>
>>> isinstance(lst, list)
True
More information about the Albatross-users
mailing list