2006年10月23日 星期一 02:18
看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的两个属性的意思: size = self.GetClientSize() glViewport( 0,0, *size) #相当于glViewport( 0,0, size.width, size.height) 请教大家*号这种用法的意思和该用在什么地方,谢谢。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061023/ab0bb46a/attachment.html
2006年10月23日 星期一 07:36
size 是一个tuple http://groups.google.com/group/python-cn/browse_thread/thread/171562599ef2f9f1/518a3f0f021e9263?lnk=gst&q;=%E4%BC%A0%E9%80%81%E5%85%83%E7%BB%84%E7%9A%84*%E8%BF%98%E5%8F%AF%E4%BB%A5%E6%94%BE%E5%85%A5%E5%87%BD%E6%95%B0%3F++&rnum;=1#518a3f0f021e9263 On 10/23/06, jiang hongbo <larkdream at gmail.com> wrote: > > 看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的两个属性的意思: > size = self.GetClientSize() > glViewport( 0,0, *size) #相当于glViewport( 0,0, size.width, > size.height) > 请教大家*号这种用法的意思和该用在什么地方,谢谢。 > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- http://codeplayer.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061023/d0d11994/attachment.html
2006年10月23日 星期一 09:31
代表这个参数是一个列表或者是元组。 2个星号代表是一个字典。 在 06-10-23,jiang hongbo<larkdream在gmail.com> 写道: > 看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的两个属性的意思: > size = self.GetClientSize() > glViewport( 0,0, *size) #相当于glViewport( 0,0, size.width, > size.height) > 请教大家*号这种用法的意思和该用在什么地方,谢谢。 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
2006年10月23日 星期一 09:58
举个例子,下面的函数fun×完成显示参数的功能:
def fun1(arg1,arg2,arg3):
print arg1,arg2,arg3,
fun1(1,2,3)
def fun2(args):
for i in args:print i,
fun2([1,2,3])
fun2([1,2,3,4])
def fun3(*args)
for i in args:print i,
fun3(1,2,3,4,5)
fun3(1,2)
比较三个函数可以看出
fun2比fun1灵活,因为fun1的参数个数固定
fun3比fun2灵活,因为fun3的多个参数都不必用序列来传递,这在实现参数个数不
定的函数时很有用;
至于**类似,只是**表示的是带关键字的参数
def fun4(**args):
for k,v in enumerate(args):
print k,v
fun4(id=1001,name='xx')
fun4(id=1001,name='xx',age=28)
马踏飞燕 写道:
> 代表这个参数是一个列表或者是元组。
> 2个星号代表是一个字典。
>
> 在 06-10-23,jiang hongbo<larkdream at gmail.com> 写道:
>
>> 看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的两个属性的意思:
>> size = self.GetClientSize()
>> glViewport( 0,0, *size) #相当于glViewport( 0,0, size.width,
>> size.height)
>> 请教大家*号这种用法的意思和该用在什么地方,谢谢。
>> _______________________________________________
>> python-chinese
>> Post: send python-chinese at lists.python.cn
>> Subscribe: send subscribe to
>> python-chinese-request at lists.python.cn
>> Unsubscribe: send unsubscribe to
>> python-chinese-request at lists.python.cn
>> Detail Info:
>> http://python.cn/mailman/listinfo/python-chinese
>>
>>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年10月23日 星期一 10:24
Õý½â£¡ Qutr£¬qutianrang在gmail.com 2006-10-23 ----- Original Message ----- From: amingsc To: python-chinese Sent: 2006-10-23, 09:58:12 Subject: Re: [python-chinese] Çë½Ì *µÄÒâ˼ ¾Ù¸öÀý×Ó£¬ÏÂÃæµÄº¯Êýfun¡ÁÍê³ÉÏÔʾ²ÎÊýµÄ¹¦ÄÜ£º def fun1(arg1,arg2,arg3): print arg1,arg2,arg3, fun1(1,2,3) def fun2(args): for i in args:print i, fun2([1,2,3]) fun2([1,2,3,4]) def fun3(*args) for i in args:print i, fun3(1,2,3,4,5) fun3(1,2) ±È½ÏÈý¸öº¯Êý¿ÉÒÔ¿´³ö fun2±Èfun1Áé»î£¬ÒòΪfun1µÄ²ÎÊý¸öÊý¹Ì¶¨ fun3±Èfun2Áé»î£¬ÒòΪfun3µÄ¶à¸ö²ÎÊý¶¼²»±ØÓÃÐòÁÐÀ´´«µÝ£¬ÕâÔÚʵÏÖ²ÎÊý¸öÊý²» ¶¨µÄº¯ÊýʱºÜÓÐÓã» ÖÁÓÚ**ÀàËÆ,Ö»ÊÇ**±íʾµÄÊÇ´ø¹Ø¼ü×ֵIJÎÊý def fun4(**args): for k,v in enumerate(args): print k,v fun4(id=1001,name='xx') fun4(id=1001,name='xx',age=28) Âí̤·ÉÑà дµÀ: > ´ú±íÕâ¸ö²ÎÊýÊÇÒ»¸öÁбí»òÕßÊÇÔª×é¡£ > 2¸öÐǺŴú±íÊÇÒ»¸ö×ֵ䡣 > > ÔÚ 06-10-23£¬jiang hongbo<larkdream在gmail.com> дµÀ£º > >> ¿´µ½ÈçÏ´úÂ룬²»Ã÷°×ÕâÀï*µÄÓ÷¨£¬ËƺõÊǽ«size·Ö½âΪÀඨÒåÖеÄÁ½¸öÊôÐÔµÄÒâ˼£º >> size = self.GetClientSize() >> glViewport( 0,0, *size) #Ï൱ÓÚglViewport( 0,0, size.width, >> size.height) >> Çë½Ì´ó¼Ò*ºÅÕâÖÖÓ÷¨µÄÒâ˼ºÍ¸ÃÓÃÔÚʲôµØ·½£¬Ð»Ð»¡£ >> _______________________________________________ >> python-chinese >> Post: send python-chinese在lists.python.cn >> Subscribe: send subscribe to >> python-chinese-request在lists.python.cn >> Unsubscribe: send unsubscribe to >> python-chinese-request在lists.python.cn >> Detail Info: >> http://python.cn/mailman/listinfo/python-chinese >> >> > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese _______________________________________________ python-chinese Post: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061023/9e93020b/attachment.html
2006年10月23日 星期一 14:41
你所讲解的,是在形参(parameters)中,这种用法是python比较基本的。
但是楼主说的情况,似乎是*用在了实参(arguments)中,似乎是把一个列表替换成其
各个成员填入形参中。这种用法,我还是第一次看到~
查了一下Python Reference Manual, 在5.3.4 Calls中,看到了这种用法:
If the syntax "*expression" appears in the function call, "expression"
must evaluate to a sequence. Elements from this sequence are treated as if
they were additional positional arguments; if there are postional
arguments x1,...,xN , and "expression" evaluates to a sequence y1,...,yM,
this is equivalent to a call with M+N positional arguments
x1,...,xN,y1,...,yM.
If the syntax "**expression" appears in the function call, "expression"
must evaluate to a (subclass of) dictionary, the contents of which are
treated as additional keyword arguments. In the case of a keyword
appearing in both "expression" and as an explicit keyword argument, a
TypeError exception is raised.
对于在函数调用中,传入实参有形为*exp的,则exp必须是一个序列类型,将exp序列
中的每一个元素按顺序填入到该函数的形参列表中:
>>> def fun(a, b, c, d):
... print a, b, c, d
...
>>> li = [7,8]
>>> fun(1,2, *li)
1 2 7 8
>>>
对于在函数调用中,传入实参有形为**exp的,则exp必须是一个字典,将exp字典的内
容,以关键字参数的方式填入函数的形参中,例子还是上面的fun()函数:
>>> di = {'b':7, 'c':8}
>>> fun(1, d=4, **di)
1 7 8 4
>>>
On Mon, 23 Oct 2006 09:58:12 +0800, amingsc
<amingsc at gmail.com> wrote:
> 举个例子,下面的函数fun×完成显示参数的功能:
> def fun1(arg1,arg2,arg3):
> print arg1,arg2,arg3,
> fun1(1,2,3)
>
> def fun2(args):
> for i in args:print i,
> fun2([1,2,3])
> fun2([1,2,3,4])
>
> def fun3(*args)
> for i in args:print i,
> fun3(1,2,3,4,5)
> fun3(1,2)
> 比较三个函数可以看出
> fun2比fun1灵活,因为fun1的参数个数固定
> fun3比fun2灵活,因为fun3的多个参数都不必用序列来传递,这在实现参数个数不
> 定的函数时很有用;
>
> 至于**类似,只是**表示的是带关键字的参数
> def fun4(**args):
> for k,v in enumerate(args):
> print k,v
> fun4(id=1001,name='xx')
> fun4(id=1001,name='xx',age=28)
>
> 马踏飞燕 写道:
>> 代表这个参数是一个列表或者是元组。
>> 2个星号代表是一个字典。
>>
>> 在 06-10-23,jiang hongbo<larkdream at gmail.com> 写道:
>>
>>> 看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的两个属性的
>>> 意思:
>>> size = self.GetClientSize()
>>> glViewport( 0,0, *size) #相当于glViewport( 0,0, size.width,
>>> size.height)
>>> 请教大家*号这种用法的意思和该用在什么地方,谢谢。
>>> _______________________________________________
>>> python-chinese
>>> Post: send python-chinese at lists.python.cn
>>> Subscribe: send subscribe to
>>> python-chinese-request at lists.python.cn
>>> Unsubscribe: send unsubscribe to
>>> python-chinese-request at lists.python.cn
>>> Detail Info:
>>> http://python.cn/mailman/listinfo/python-chinese
>>>
>>>
>> _______________________________________________
>> python-chinese
>> Post: send python-chinese at lists.python.cn
>> Subscribe: send subscribe to python-chinese-request at lists.python.cn
>> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
>> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
2006年10月23日 星期一 14:54
thank you On 10/23/06, Leira Hua <lhua在altigen.com.cn> wrote: > > 你所讲解的,是在形参(parameters)中,这种用法是python比较基本的。 > 但是楼主说的情况,似乎是*用在了实参(arguments)中,似乎是把一个列表替换成其 > 各个成员填入形参中。这种用法,我还是第一次看到~ > > 查了一下Python Reference Manual, 在5.3.4 Calls中,看到了这种用法: > > If the syntax "*expression" appears in the function call, "expression" > must evaluate to a sequence. Elements from this sequence are treated as if > they were additional positional arguments; if there are postional > arguments x1,...,xN , and "expression" evaluates to a sequence y1,...,yM, > this is equivalent to a call with M+N positional arguments > x1,...,xN,y1,...,yM. > > If the syntax "**expression" appears in the function call, "expression" > must evaluate to a (subclass of) dictionary, the contents of which are > treated as additional keyword arguments. In the case of a keyword > appearing in both "expression" and as an explicit keyword argument, a > TypeError exception is raised. > > > 对于在函数调用中,传入实参有形为*exp的,则exp必须是一个序列类型,将exp序列 > 中的每一个元素按顺序填入到该函数的形参列表中: > >>> def fun(a, b, c, d): > ... print a, b, c, d > ... > >>> li = [7,8] > >>> fun(1,2, *li) > 1 2 7 8 > >>> > > 对于在函数调用中,传入实参有形为**exp的,则exp必须是一个字典,将exp字典的内 > 容,以关键字参数的方式填入函数的形参中,例子还是上面的fun()函数: > >>> di = {'b':7, 'c':8} > >>> fun(1, d=4, **di) > 1 7 8 4 > >>> > > > On Mon, 23 Oct 2006 09:58:12 +0800, amingsc > <amingsc在gmail.com> wrote: > > > 举个例子,下面的函数fun×完成显示参数的功能: > > def fun1(arg1,arg2,arg3): > > print arg1,arg2,arg3, > > fun1(1,2,3) > > > > def fun2(args): > > for i in args:print i, > > fun2([1,2,3]) > > fun2([1,2,3,4]) > > > > def fun3(*args) > > for i in args:print i, > > fun3(1,2,3,4,5) > > fun3(1,2) > > 比较三个函数可以看出 > > fun2比fun1灵活,因为fun1的参数个数固定 > > fun3比fun2灵活,因为fun3的多个参数都不必用序列来传递,这在实现参数个数不 > > 定的函数时很有用; > > > > 至于**类似,只是**表示的是带关键字的参数 > > def fun4(**args): > > for k,v in enumerate(args): > > print k,v > > fun4(id=1001,name='xx') > > fun4(id=1001,name='xx',age=28) > > > > 马踏飞燕 写道: > >> 代表这个参数是一个列表或者是元组。 > >> 2个星号代表是一个字典。 > >> > >> 在 06-10-23,jiang hongbo<larkdream在gmail.com> 写道: > >> > >>> 看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的两个属性的 > >>> 意思: > >>> size = self.GetClientSize() > >>> glViewport( 0,0, *size) #相当于glViewport( 0,0, size.width, > >>> size.height) > >>> 请教大家*号这种用法的意思和该用在什么地方,谢谢。 > >>> _______________________________________________ > >>> python-chinese > >>> Post: send python-chinese在lists.python.cn > >>> Subscribe: send subscribe to > >>> python-chinese-request在lists.python.cn > >>> Unsubscribe: send unsubscribe to > >>> python-chinese-request在lists.python.cn > >>> Detail Info: > >>> http://python.cn/mailman/listinfo/python-chinese > >>> > >>> > >> _______________________________________________ > >> python-chinese > >> Post: send python-chinese在lists.python.cn > >> Subscribe: send subscribe to python-chinese-request在lists.python.cn > >> Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > >> Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061023/b28e86ed/attachment-0001.html
2006年10月23日 星期一 15:22
ÔÚʵ²ÎÖÐͨ¹ý*ÔËËã·û½â¹¹´«µÝÒ»¸öÐòÁлòÕßͨ¹ý**ÔËËã·û´«µÝÒ»¸ö×ֵ䣬ÊÇÎÒ¾³£×öµÄÊÂÇ飺£©¡£ 2006/10/23, Leira Hua <lhua在altigen.com.cn>: > > ÄãËù½²½âµÄ,ÊÇÔÚÐβÎ(parameters)ÖУ¬ÕâÖÖÓ÷¨ÊÇpython±È½Ï»ù±¾µÄ¡£ > µ«ÊÇÂ¥Ö÷˵µÄÇé¿ö£¬ËƺõÊÇ*ÓÃÔÚÁËʵ²Î(arguments)ÖУ¬ËƺõÊǰÑÒ»¸öÁбíÌæ»»³ÉÆä > ¸÷¸ö³ÉÔ±ÌîÈëÐβÎÖС£ÕâÖÖÓ÷¨£¬ÎÒ»¹ÊǵÚÒ»´Î¿´µ½~ > > ²éÁËÒ»ÏÂPython Reference Manual£¬ ÔÚ5.3.4 CallsÖУ¬¿´µ½ÁËÕâÖÖÓ÷¨£º > > If the syntax "*expression" appears in the function call, "expression" > must evaluate to a sequence. Elements from this sequence are treated as if > they were additional positional arguments; if there are postional > arguments x1,...,xN , and "expression" evaluates to a sequence y1,...,yM, > this is equivalent to a call with M+N positional arguments > x1,...,xN,y1,...,yM. > > If the syntax "**expression" appears in the function call, "expression" > must evaluate to a (subclass of) dictionary, the contents of which are > treated as additional keyword arguments. In the case of a keyword > appearing in both "expression" and as an explicit keyword argument, a > TypeError exception is raised. > > > ¶ÔÓÚÔÚº¯Êýµ÷ÓÃÖУ¬´«Èëʵ²ÎÓÐÐÎΪ*expµÄ£¬Ôòexp±ØÐëÊÇÒ»¸öÐòÁÐÀàÐÍ£¬½«expÐòÁÐ > ÖеÄÿһ¸öÔªËØ°´Ë³ÐòÌîÈëµ½¸Ãº¯ÊýµÄÐβÎÁбíÖУº > >>> def fun(a, b, c, d): > ... print a, b, c, d > ... > >>> li = [7,8] > >>> fun(1,2, *li) > 1 2 7 8 > >>> > > ¶ÔÓÚÔÚº¯Êýµ÷ÓÃÖУ¬´«Èëʵ²ÎÓÐÐÎΪ**expµÄ£¬Ôòexp±ØÐëÊÇÒ»¸ö×ֵ䣬½«exp×ÖµäµÄÄÚ > ÈÝ£¬ÒԹؼü×Ö²ÎÊýµÄ·½Ê½ÌîÈ뺯ÊýµÄÐβÎÖУ¬Àý×Ó»¹ÊÇÉÏÃæµÄfun()º¯Êý£º > >>> di = {'b':7, 'c':8} > >>> fun(1, d=4, **di) > 1 7 8 4 > >>> > > > On Mon, 23 Oct 2006 09:58:12 +0800, amingsc > <amingsc在gmail.com> wrote: > > > ¾Ù¸öÀý×Ó£¬ÏÂÃæµÄº¯Êýfun¡ÁÍê³ÉÏÔʾ²ÎÊýµÄ¹¦ÄÜ£º > > def fun1(arg1,arg2,arg3): > > print arg1,arg2,arg3, > > fun1(1,2,3) > > > > def fun2(args): > > for i in args:print i, > > fun2([1,2,3]) > > fun2([1,2,3,4]) > > > > def fun3(*args) > > for i in args:print i, > > fun3(1,2,3,4,5) > > fun3(1,2) > > ±È½ÏÈý¸öº¯Êý¿ÉÒÔ¿´³ö > > fun2±Èfun1Áé»î£¬ÒòΪfun1µÄ²ÎÊý¸öÊý¹Ì¶¨ > > fun3±Èfun2Áé»î£¬ÒòΪfun3µÄ¶à¸ö²ÎÊý¶¼²»±ØÓÃÐòÁÐÀ´´«µÝ£¬ÕâÔÚʵÏÖ²ÎÊý¸öÊý²» > > ¶¨µÄº¯ÊýʱºÜÓÐÓã» > > > > ÖÁÓÚ**ÀàËÆ,Ö»ÊÇ**±íʾµÄÊÇ´ø¹Ø¼ü×ֵIJÎÊý > > def fun4(**args): > > for k,v in enumerate(args): > > print k,v > > fun4(id=1001,name='xx') > > fun4(id=1001,name='xx',age=28) > > > > Âí̤·ÉÑà дµÀ: > >> ´ú±íÕâ¸ö²ÎÊýÊÇÒ»¸öÁбí»òÕßÊÇÔª×é¡£ > >> 2¸öÐǺŴú±íÊÇÒ»¸ö×ֵ䡣 > >> > >> ÔÚ 06-10-23£¬jiang hongbo<larkdream在gmail.com> дµÀ£º > >> > >>> ¿´µ½ÈçÏ´úÂ룬²»Ã÷°×ÕâÀï*µÄÓ÷¨£¬ËƺõÊǽ«size·Ö½âΪÀඨÒåÖеÄÁ½¸öÊôÐ﵀ > >>> Òâ˼£º > >>> size = self.GetClientSize() > >>> glViewport( 0,0, *size) #Ï൱ÓÚglViewport( 0,0, size.width, > >>> size.height) > >>> Çë½Ì´ó¼Ò*ºÅÕâÖÖÓ÷¨µÄÒâ˼ºÍ¸ÃÓÃÔÚʲôµØ·½£¬Ð»Ð»¡£ > >>> _______________________________________________ > >>> python-chinese > >>> Post: send python-chinese在lists.python.cn > >>> Subscribe: send subscribe to > >>> python-chinese-request在lists.python.cn > >>> Unsubscribe: send unsubscribe to > >>> python-chinese-request在lists.python.cn > >>> Detail Info: > >>> http://python.cn/mailman/listinfo/python-chinese > >>> > >>> > >> _______________________________________________ > >> python-chinese > >> Post: send python-chinese在lists.python.cn > >> Subscribe: send subscribe to python-chinese-request在lists.python.cn > >> Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > >> Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- »¶Ó·ÃÎÊ£º http://blog.csdn.net/ccat ÁõöÎ March.Liu -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061023/64357829/attachment.htm
2006年10月23日 星期一 16:45
哈哈,这样的啊,俺大意了,不过其实是一回事,反正是函数进行参数传递时进行
的特殊处理罢了,这个特殊处理也就是用“*序列”来将序列解包成序列中的各个元
素,以实现形参与实参的匹配及传递,这两种情况也就是“*”操作分别出现在形参
与实参的区别啦!
FUN(*实参 ~ 形参)
FUN( 实参 ~ *形参)
python不加这个*也是完全可以的,只不过加上就使得用户不需要自己编码进行解
包,用起来更省事,这正体现了python本身的宗旨--让一切变得更简单!
例子:
def fun1(a,b,c):
print a,b,c
def fun2(*abc):
for i in abc:print i,
>>> fun1(*[1,2,3])
1 2 3
>>> fun1(**{'a':1,'b':2,'c':3})
1 2 3
>>> fun1(**{'a':1,'c':3,'b':2})
1 2 3
>>> fun2( 1,2,3 )
1 2 3
fdcn 写道:
> thank you
>
> On 10/23/06, *Leira Hua* <lhua at altigen.com.cn
> lhua at altigen.com.cn>> wrote:
>
> 你所讲解的,是在形参(parameters)中,这种用法是python比较基本的。
> 但是楼主说的情况,似乎是*用在了实参(arguments)中,似乎是把一个列表
> 替换成其
> 各个成员填入形参中。这种用法,我还是第一次看到~
>
> 查了一下Python Reference Manual, 在5.3.4 Calls中,看到了这种用法:
>
> If the syntax "*expression" appears in the function call,
> "expression"
> must evaluate to a sequence. Elements from this sequence are
> treated as if
> they were additional positional arguments; if there are postional
> arguments x1,...,xN , and "expression" evaluates to a sequence
> y1,...,yM,
> this is equivalent to a call with M+N positional arguments
> x1,...,xN,y1,...,yM.
>
> If the syntax "**expression" appears in the function call,
> "expression"
> must evaluate to a (subclass of) dictionary, the contents of which
> are
> treated as additional keyword arguments. In the case of a keyword
> appearing in both "expression" and as an explicit keyword argument, a
> TypeError exception is raised.
>
>
> 对于在函数调用中,传入实参有形为*exp的,则exp必须是一个序列类型,
> 将exp序列
> 中的每一个元素按顺序填入到该函数的形参列表中:
> >>> def fun(a, b, c, d):
> ... print a, b, c, d
> ...
> >>> li = [7,8]
> >>> fun(1,2, *li)
> 1 2 7 8
> >>>
>
> 对于在函数调用中,传入实参有形为**exp的,则exp必须是一个字典,将
> exp字典的内
> 容,以关键字参数的方式填入函数的形参中,例子还是上面的fun()函数:
> >>> di = {'b':7, 'c':8}
> >>> fun(1, d=4, **di)
> 1 7 8 4
> >>>
>
>
> On Mon, 23 Oct 2006 09:58:12 +0800, amingsc
> < amingsc at gmail.com amingsc at gmail.com>> wrote:
>
> > 举个例子,下面的函数fun×完成显示参数的功能:
> > def fun1(arg1,arg2,arg3):
> > print arg1,arg2,arg3,
> > fun1(1,2,3)
> >
> > def fun2(args):
> > for i in args:print i,
> > fun2([1,2,3])
> > fun2([1,2,3,4])
> >
> > def fun3(*args)
> > for i in args:print i,
> > fun3(1,2,3,4,5)
> > fun3(1,2)
> > 比较三个函数可以看出
> > fun2比fun1灵活,因为fun1的参数个数固定
> > fun3比fun2灵活,因为fun3的多个参数都不必用序列来传递,这在实现参
> 数个数不
> > 定的函数时很有用;
> >
> > 至于**类似,只是**表示的是带关键字的参数
> > def fun4(**args):
> > for k,v in enumerate(args):
> > print k,v
> > fun4(id=1001,name='xx')
> > fun4(id=1001,name='xx',age=28)
> >
> > 马踏飞燕 写道:
> >> 代表这个参数是一个列表或者是元组。
> >> 2个星号代表是一个字典。
> >>
> >> 在 06-10-23,jiang hongbo<larkdream at gmail.com
> larkdream at gmail.com>> 写道:
> >>
> >>> 看到如下代码,不明白这里*的用法,似乎是将size分解为类定义中的
> 两个属性的
> >>> 意思:
> >>> size = self.GetClientSize()
> >>> glViewport( 0,0, *size) #相当于glViewport( 0,0,
> size.width,
> >>> size.height)
> >>> 请教大家*号这种用法的意思和该用在什么地方,谢谢。
> >>> _______________________________________________
> >>> python-chinese
> >>> Post: send python-chinese at lists.python.cn
> python-chinese at lists.python.cn>
> >>> Subscribe: send subscribe to
> >>> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> >>> Unsubscribe: send unsubscribe to
> >>> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> >>> Detail Info:
> >>> http://python.cn/mailman/listinfo/python-chinese
> >>>
> >>>
> >> _______________________________________________
> >> python-chinese
> >> Post: send python-chinese at lists.python.cn
> python-chinese at lists.python.cn>
> >> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> >> Unsubscribe: send unsubscribe
> to python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> >> Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> python-chinese at lists.python.cn>
> > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> > Unsubscribe: send unsubscribe
> to python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> python-chinese at lists.python.cn>
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> Unsubscribe: send unsubscribe
> to python-chinese-request at lists.python.cn
> python-chinese-request at lists.python.cn>
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
> <http://python.cn/mailman/listinfo/python-chinese>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年10月24日 星期二 02:33
谢谢大家,这么一说俺就明白了。 以前就知道*做形参的时候的用法,没想到做实参时也可以这么用。和形参的这种用法结合,可以派上不少用场啊,比C语言的不定参数用法要灵活,更加动态。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061024/990707b1/attachment.html
Zeuux © 2026
京ICP备05028076号