PHP list()在Python中等价

2024-09-30 14:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

python中是否有与PHP list()函数等效的函数?例如:

菲律宾比索:

list($first, $second, $third) = $myIndexArray;
echo "First: $first, Second: $second";

Tags: 函数echolistfirstphpsecondthird比索
1条回答
网友
1楼 · 发布于 2024-09-30 14:26:28
>>> a, b, c = [1, 2, 3]
>>> print a, b, c
1 2 3

直接或直接翻译:

^{pr2}$

Python通过调用右侧表达式上的__iter__方法并将每个项分配给左侧的变量来实现此功能。这允许您定义如何将自定义对象解压为多变量赋值:

>>> class MyClass(object):
...   def __iter__(self):
...     return iter([1, 2, 3])
... 
>>> a, b, c = MyClass()
>>> print a, b, c
1 2 3

相关问题 更多 >