class ListObject(object): items = ((1, 'one'), (2, 'two'), (3, 'three')) def get_num(self, item): return item[0] def get_text(self, item): return item[1] def get_list(self, fn=lambda s, i: ListObject.get_num(s, i)): return [fn(self, j) for j in self.items] lo = ListObject() print 'numbers:', lo.get_list() print 'texts:', lo.get_list(ListObject.get_text) print 'texts:', lo.get_list(lambda s, i: i[1]) print 'items:', lo.get_list(lambda s, i: i)When you run this piece of code you get what you want:
numbers: [1, 2, 3] texts: ['one', 'two', 'three'] texts: ['one', 'two', 'three'] items: [(1, 'one'), (2, 'two'), (3, 'three')]By default, get_list method uses get_num method and apply it to all items. But you can supply your own fucntion to apply it on items.
Of course, this is very stupid example but it shows the principle. And this principle is quite handy for UI automation I am currently working on :-)
No comments:
Post a Comment