4 ms·
Short answer: not really. Long answer: In [1]: from operator import attrgetter In [2]: class Person: ...: def __init__(self, name): ...:
by walrus 14y ago
Short answer: not really.
Long answer:
In [1]: from operator import attrgetter
In [2]: class Person:
...: def __init__(self, name):
...: self.name = name
...:
In [3]: me = Person('walrus')
In [4]: getattr(me, 'name')
Out[4]: 'walrus'
In [5]: attrgetter('name')(me)
Out[5]: 'walrus'
This is potentially useful if you're using `map`:
In [6]: people = [me, Person('aschwo')]
In [7]: list(map(lambda obj: getattr(obj, 'name'), people))
Out[7]: ['walrus', 'aschwo']
In [8]: list(map(attrgetter('name'), people))
Out[8]: ['walrus', 'aschwo']
Really, though, the functional programming idioms don't meld too well with Python. I think this is a lot nicer:
In [9]: [person.name for person in people]
Out[9]: ['walrus', 'aschwo']
- andreasvc 14y agoOn the last point, a list comprehension is just a much a functional programming idiom--Python got them from Haskell! I use both in my code, whichever is shortest. PS: map returns a list, no need to call list() on it.
- walrus 14y agoIn Python 3, map returns a iterator, not a list: In [1]: sq = lambda x: x * x In [2]: map(sq, [1,2,3,4]) Out[2]: <builtins.map at 0x2135050> In [3]: list(_) Out[3]: [1, 4, 9, 16]