lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Jun 2, 2014 at 4:16 PM, Coda Highland <chighland@gmail.com> wrote:
> On Mon, Jun 2, 2014 at 12:12 PM, Petite Abeille
> <petite.abeille@gmail.com> wrote:
>> Multiple return values going mainstream:
>>
>> https://developer.apple.com/swift/
>>
>> Yeah :D
>
> Python's not mainstream?
>
> /s/ Adam
>


Well, it's a bit OT and nitpicking, but in Python when you do this:

def foo(a, b):
  return a, b

you are actually returning only one value, (a, b), which is an
instance of tuple [1]  (parenthesis is optional for tuple literals).

And when you do this:

a, b = foo()

you are actually unpacking that tuple [2].

So it would be the equivalent in Lua:

function foo(a,b) return {a,b} end
a,b = table.unpack(foo())


[1]
Python 3.4.1 (default, May 19 2014, 17:40:19)
[GCC 4.9.0 20140507 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def foo(a,b): return a,b
>>> type(foo(1,2))
<class 'tuple'>
>>> dis.dis(foo)
  1           0 LOAD_FAST                0 (a)
              3 LOAD_FAST                1 (b)
              6 BUILD_TUPLE              2
              9 RETURN_VALUE


[2]
>>> dis.dis("a,b = foo()")
  1           0 LOAD_NAME                0 (foo)
              3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
              6 UNPACK_SEQUENCE          2
              9 STORE_NAME               1 (a)
             12 STORE_NAME               2 (b)
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE


-- 
NI!

() - www.asciiribbon.org
/\ - ascii ribbon campaign against html e-mail and proprietary attachments