List comprehension returning subclassed list type?

bullockbefriending bard kinch1967 at gmail.com
Sun Mar 25 02:43:10 EDT 2007


Given:

class Z(object):
    various defs, etc.

class ZList(list):
    various defs, etc.

i would like to be able to replace

z_list = ZList()
for y in list_of_objects_of_class_Y:
    z_list.append(y)


with something like this:

z_list = [Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y]

Of course this just gives me a plain list and no access to the
methodsof z_list. I could, of course go and write a static method in
ZList which takes a plain list of Z objects and returns a ZList.

Anyway, my question is whether or not this can be done more elegantly
via list comprehension?




More information about the Python-list mailing list