3
A
回答
7
不完全是短期的最短途径,但如果我理解你正确的,这将工作:
a, b, c = (x.method() for x in (a, b, c))
(注意,这如果a
,b
或c
指的是同一个对象,该方法将被调用几次)。
5
不短。但是这里有一个使用operator.methodcaller
的替代方案。
a, b, c = map(operator.methodcaller('method'), (a,b,c))
例子:
>>> import operator
>>> a, b, c = [1,2,3], [4,5,6], [7,8,9]
>>> a, b, c = map(operator.methodcaller('pop'), (a,b,c))
>>> a
3
>>> b
6
>>> c
9
>>> m = operator.methodcaller('pop') # If you call `method` multiple times,
# keep a reference to the methodcaller
>>> a, b, c = [1,2,3], [4,5,6], [7,8,9]
>>> a, b, c = map(m, (a,b,c))
>>> a
3
>>> b
6
>>> c
9
如果所有的对象都是同一类型的,你可以使用未绑定的方法。例如a
,b
,c
这里是所有列表实例。所以你可以使用list.pop
而不是operator.methodcaller('pop')
。
例如:
>>> a, b, c = [1,2,3], [4,5,6], [7,8,9] # a, b, c are all `list` objects.
>>> a, b, c = map(list.pop, (a,b,c))
>>> a
3
>>> b
6
>>> c
9
1
相关问题
- 1. 在Python中对类方法调用mapPartition
- 2. 在python中调用方法
- 3. 调用父方法在Python
- 4. @在python方法调用
- 5. 整数对象的Python调用方法
- 6. 调用python方法
- 7. 当类正在调用对方的方法时,Python/Tkinter失败
- 8. 无法在Python中调用方法
- 9. 在Python中使用getattr调用方法
- 10. Python的调用方法在Windows在cmd
- 11. 如何在Python中用对象的模拟方法调用self?
- 12. Odoo qweb调用Python方法
- 13. Python拦截方法调用
- 14. [Python]调用方法(self)
- 15. 从java调用python方法
- 16. Python - 调用方法在子类
- 17. 在Python/PySide中调用类方法
- 18. 调用在python不同的方法
- 19. 在Python中调用的方法
- 20. 链式方法调用在Python
- 21. 嘲讽方法在Python中调用
- 22. 在Python中调用父类的方法
- 23. 调用一个类的方法在python
- 24. Telegram.org API:在Python中调用方法invokeWithLayer
- 25. 在Python中调用另一种方法
- 26. Python:在默认对象上调用方法
- 27. 如何捕获在python中调用对象的任何方法?
- 28. 动态方法调用在Python 2.7使用方法名
- 29. 调用方法对象
- 30. 对象的调用方法
好吧,是'A,B,C =(x.method()for x in(a,b,c))'够了吗? – bereal
为了清楚起见,您想要将每个对象调用'method()'的结果返回给对象本身? –
为什么不使用列表? 'l2 = [x.method()for x in l1]'可能是一个很好的方法来做你需要的。 – user2357112