nikoo 发表于 2017-5-6 11:32:05

Python的一个小技巧和Lua的Unpack比较

Python的一个小技巧和Lua的Unpack比较
<meta content="SciTE - www.Scintilla.org" name="Generator" id="fegt0"><script type="text/javascript" language="JavaScript" id="fegt1"><!--function symbol(id, sym) {if (id.textContent==undefined) {id.innerText=sym; } else {id.textContent=sym; }}function toggle(id) {var thislayer=document.getElementById('ln'+id);id-=1;var togline=document.getElementById('hd'+id);var togsym=document.getElementById('bt'+id);if (thislayer.style.display == 'none') {thislayer.style.display='block';togline.style.textDecoration='none';symbol(togsym,'- ');} else {thislayer.style.display='none';togline.style.textDecoration='underline';symbol(togsym,'+ ');}}//--></script><style type="text/css" id="fegt2">.S0 {color: #808080;}.S2 {color: #007F7F;}.S5 {font-weight: bold;color: #00007F;}.S8 {font-weight: bold;color: #0000FF;}.S9 {font-weight: bold;color: #007F7F;}.S10 {font-weight: bold;color: #000000;}span {font-family: 'Verdana';color: #000000;font-size: 10pt;}</style>- classA(object):
- defsetline(self,line):
self.line=line

- defsetitem(self,a,b):
printa,b

- deff(self):
    print(type(self.line[:2]))   
   self.setitem(*self.line[:2]) # 这里的*表示把一个list解成两个参数

a=A()
a.setline()
a.f()
输出:
>pythonw -u "t.py"
<type 'list'>
1 2

对应之Lua的用法:
<meta content="SciTE - www.Scintilla.org" name="Generator" id="jnak1"><script type="text/javascript" language="JavaScript" id="jnak2"><!--function symbol(id, sym) {if (id.textContent==undefined) {id.innerText=sym; } else {id.textContent=sym; }}function toggle(id) {var thislayer=document.getElementById('ln'+id);id-=1;var togline=document.getElementById('hd'+id);var togsym=document.getElementById('bt'+id);if (thislayer.style.display == 'none') {thislayer.style.display='block';togline.style.textDecoration='none';symbol(togsym,'- ');} else {thislayer.style.display='none';togline.style.textDecoration='underline';symbol(togsym,'+ ');}}//--></script><style type="text/css" id="jnak3">.S0 {color: #FF0000;}.S4 {color: #007F7F;}.S5 {color: #00007F;}.S10 {color: #000000;}.S13 {color: #00007F;background: #F5FFF5;text-decoration: inherit;}span {font-family: 'Verdana';color: #000000;font-size: 10pt;}</style>- f=function(...)
print(...)
end

t={1,2}
f(unpack(t))

输出:
>luai "t.lua"
1 2
页: [1]
查看完整版本: Python的一个小技巧和Lua的Unpack比较