11
pythonのPHP list()関数に相当するものはありますか?たとえば、次のようにPythonのPHP list()と同等です
PHP:
list($first, $second, $third) = $myIndexArray;
echo "First: $first, Second: $second";
pythonのPHP list()関数に相当するものはありますか?たとえば、次のようにPythonのPHP list()と同等です
PHP:
list($first, $second, $third) = $myIndexArray;
echo "First: $first, Second: $second";
>>> a, b, c = [1, 2, 3]
>>> print a, b, c
1 2 3
またはあなたの例直接翻訳:
>>> myIndexArray = [1, 2, 3]
>>> first, second, third = myIndexArray
>>> print "First: %d, Second: %d" % (first, second)
First: 1, Second: 2
Pythonは右側に__iter__
メソッドを呼び出すことで、この機能を実装各項目を左側の変数に割り当てます。これにより、カスタムオブジェクトを複数変数代入にどのように展開するかを定義することができます。
>>> class MyClass(object):
... def __iter__(self):
... return iter([1, 2, 3])
...
>>> a, b, c = MyClass()
>>> print a, b, c
1 2 3
甘いです。それは私が必要としていたよりもずっと多くの情報です、そしてそれは最高です! – Drew