Python论坛  - 讨论区

标题:[python-chinese] 如何写出具有可变参数的函数

2005年07月08日 星期五 17:29

guqi guqi at oasisnet.com.cn
Fri Jul 8 17:29:40 HKT 2005

例如:
定义时func(a,b,c)

调用时func(a)或func(a,b)呢.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050708/f1735197/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月08日 星期五 17:33

limodou limodou at gmail.com
Fri Jul 8 17:33:22 HKT 2005

必输参数不能省,不定参数前面加*,如:
func(a, *args)
这样可以使用
func(a)
func(a, b)
func(a, b, c)
...
进行调用

在 05-7-8,guqi<guqi at oasisnet.com.cn> 写道:
>  
> 例如: 
> 定义时func(a,b,c) 
>   
> 调用时func(a)或func(a,b)呢. 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 


-- 
I like python! 
My Donews Blog: http://www.donews.net/limodou
New Google Maillist: http://groups-beta.google.com/group/python-cn

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月08日 星期五 17:43

Qiangning Hong hongqn at gmail.com
Fri Jul 8 17:43:42 HKT 2005

On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
>  
> 例如: 
> 定义时func(a,b,c) 
>   
> 调用时func(a)或func(a,b)呢. 

用缺省参数或者不定长参数列表
请看python自带的教程里4.7节"More on Defining Functions"
http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
   -- Sybren Stuvel @ c.l.python

Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月11日 星期一 00:38

guqi guqi at oasisnet.com.cn
Mon Jul 11 00:38:47 HKT 2005

给个简单一点的例子.

----- Original Message ----- 
From: "Qiangning Hong" <hongqn at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Friday, July 08, 2005 5:43 PM
Subject: Re: [python-chinese] 如何写出具有可变参数的函数


> On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
> >  
> > 例如: 
> > 定义时func(a,b,c) 
> >   
> > 调用时func(a)或func(a,b)呢. 
> 
> 用缺省参数或者不定长参数列表
> 请看python自带的教程里4.7节"More on Defining Functions"
> http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000
> 
> -- 
> Qiangning Hong
> 
> I'm usually annoyed by IDEs because, for instance, they don't use VIM
> as an editor. Since I'm hooked to that, all IDEs I've used so far have
> failed to impress me.
>    -- Sybren Stuvel @ c.l.python
> 
> Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> 


--------------------------------------------------------------------------------


> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月11日 星期一 02:00

Qiangning Hong hongqn at gmail.com
Mon Jul 11 02:00:56 HKT 2005

用缺省函数:

def func(a, b=1, c=2):
   print a, b, c

func(5) 输出 5 1 2
func(5, 3) 输出 5 3 2
func(5, 3, 4) 输出 5 3 4


用不定长参数列表

def func(a, *args):
   print a, args

func(5) 输出 5 ()
func(5, 3) 输出 5 (3,)
func(5, 3, 4) 输出 5 (3, 4)


guqi wrote:
> 给个简单一点的例子.
> 
> ----- Original Message ----- 
> From: "Qiangning Hong" <hongqn at gmail.com>
> To: <python-chinese at lists.python.cn>
> Sent: Friday, July 08, 2005 5:43 PM
> Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> 
> 
> 
>>On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
>>
>>> 
>>>例如: 
>>>定义时func(a,b,c) 
>>>  
>>>调用时func(a)或func(a,b)呢. 
>>
>>用缺省参数或者不定长参数列表
>>请看python自带的教程里4.7节"More on Defining Functions"
>>http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000
>>
>>-- 
>>Qiangning Hong
>>
>>I'm usually annoyed by IDEs because, for instance, they don't use VIM
>>as an editor. Since I'm hooked to that, all IDEs I've used so far have
>>failed to impress me.
>>   -- Sybren Stuvel @ c.l.python
>>
>>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
>>
> 
> 
> --------------------------------------------------------------------------------
> 
> 
> 
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese


-- 
Qiangning

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月11日 星期一 09:30

梅劲松  stephen.cn at gmail.com
Mon Jul 11 09:30:32 HKT 2005

建议使用
用缺省函数:

def func(a, b=1, c=2):
  print a, b, c

func(5) 输出 5 1 2
func(5, 3) 输出 5 3 2
func(5, 3, 4) 输出 5 3 4

这样的做法。

在 05-7-11,Qiangning Hong<hongqn at gmail.com> 写道:
> 用缺省函数:
> 
> def func(a, b=1, c=2):
>   print a, b, c
> 
> func(5) 输出 5 1 2
> func(5, 3) 输出 5 3 2
> func(5, 3, 4) 输出 5 3 4
> 
> 
> 用不定长参数列表
> 
> def func(a, *args):
>   print a, args
> 
> func(5) 输出 5 ()
> func(5, 3) 输出 5 (3,)
> func(5, 3, 4) 输出 5 (3, 4)
> 
> 
> guqi wrote:
> > 给个简单一点的例子.
> >
> > ----- Original Message -----
> > From: "Qiangning Hong" <hongqn at gmail.com>
> > To: <python-chinese at lists.python.cn>
> > Sent: Friday, July 08, 2005 5:43 PM
> > Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> >
> >
> >
> >>On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
> >>
> >>>
> >>>例如:
> >>>定义时func(a,b,c)
> >>>
> >>>调用时func(a)或func(a,b)呢.
> >>
> >>用缺省参数或者不定长参数列表
> >>请看python自带的教程里4.7节"More on Defining Functions"
> >>http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000
> >>
> >>--
> >>Qiangning Hong
> >>
> >>I'm usually annoyed by IDEs because, for instance, they don't use VIM
> >>as an editor. Since I'm hooked to that, all IDEs I've used so far have
> >>failed to impress me.
> >>   -- Sybren Stuvel @ c.l.python
> >>
> >>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> >>
> >
> >
> > --------------------------------------------------------------------------------
> >
> >
> >
> >>_______________________________________________
> >>python-chinese list
> >>python-chinese at lists.python.cn
> >>http://python.cn/mailman/listinfo/python-chinese
> 
> 
> --
> Qiangning
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
梅劲松

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月11日 星期一 10:33

guqi guqi at oasisnet.com.cn
Mon Jul 11 10:33:30 HKT 2005

有人提供用*args,这是如何使用的呢.

----- Original Message ----- 
From: "梅劲松" <stephen.cn at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Monday, July 11, 2005 9:30 AM
Subject: Re: [python-chinese] 如何写出具有可变参数的函数


> 建议使用
> 用缺省函数:
> 
> def func(a, b=1, c=2):
>   print a, b, c
> 
> func(5) 输出 5 1 2
> func(5, 3) 输出 5 3 2
> func(5, 3, 4) 输出 5 3 4
> 
> 这样的做法。
> 
> 在 05-7-11,Qiangning Hong<hongqn at gmail.com> 写道:
> > 用缺省函数:
> > 
> > def func(a, b=1, c=2):
> >   print a, b, c
> > 
> > func(5) 输出 5 1 2
> > func(5, 3) 输出 5 3 2
> > func(5, 3, 4) 输出 5 3 4
> > 
> > 
> > 用不定长参数列表
> > 
> > def func(a, *args):
> >   print a, args
> > 
> > func(5) 输出 5 ()
> > func(5, 3) 输出 5 (3,)
> > func(5, 3, 4) 输出 5 (3, 4)
> > 
> > 
> > guqi wrote:
> > > 给个简单一点的例子.
> > >
> > > ----- Original Message -----
> > > From: "Qiangning Hong" <hongqn at gmail.com>
> > > To: <python-chinese at lists.python.cn>
> > > Sent: Friday, July 08, 2005 5:43 PM
> > > Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> > >
> > >
> > >
> > >>On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
> > >>
> > >>>
> > >>>例如:
> > >>>定义时func(a,b,c)
> > >>>
> > >>>调用时func(a)或func(a,b)呢.
> > >>
> > >>用缺省参数或者不定长参数列表
> > >>请看python自带的教程里4.7节"More on Defining Functions"
> > >>http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000
> > >>
> > >>--
> > >>Qiangning Hong
> > >>
> > >>I'm usually annoyed by IDEs because, for instance, they don't use VIM
> > >>as an editor. Since I'm hooked to that, all IDEs I've used so far have
> > >>failed to impress me.
> > >>   -- Sybren Stuvel @ c.l.python
> > >>
> > >>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> > >>
> > >
> > >
> > > --------------------------------------------------------------------------------
> > >
> > >
> > >
> > >>_______________________________________________
> > >>python-chinese list
> > >>python-chinese at lists.python.cn
> > >>http://python.cn/mailman/listinfo/python-chinese
> > 
> > 
> > --
> > Qiangning
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> > 
> 
> 
> -- 
> 梅劲松
> 


--------------------------------------------------------------------------------


> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月11日 星期一 10:56

limodou limodou at gmail.com
Mon Jul 11 10:56:50 HKT 2005

*args是任意长的参数。

在 05-7-11,guqi<guqi at oasisnet.com.cn> 写道:
> 有人提供用*args,这是如何使用的呢.
> 
> ----- Original Message -----
> From: "梅劲松" <stephen.cn at gmail.com>
> To: <python-chinese at lists.python.cn>
> Sent: Monday, July 11, 2005 9:30 AM
> Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> 
> 
> > 建议使用
> > 用缺省函数:
> >
> > def func(a, b=1, c=2):
> >   print a, b, c
> >
> > func(5) 输出 5 1 2
> > func(5, 3) 输出 5 3 2
> > func(5, 3, 4) 输出 5 3 4
> >
> > 这样的做法。
> >
> > 在 05-7-11,Qiangning Hong<hongqn at gmail.com> 写道:
> > > 用缺省函数:
> > >
> > > def func(a, b=1, c=2):
> > >   print a, b, c
> > >
> > > func(5) 输出 5 1 2
> > > func(5, 3) 输出 5 3 2
> > > func(5, 3, 4) 输出 5 3 4
> > >
> > >
> > > 用不定长参数列表
> > >
> > > def func(a, *args):
> > >   print a, args
> > >
> > > func(5) 输出 5 ()
> > > func(5, 3) 输出 5 (3,)
> > > func(5, 3, 4) 输出 5 (3, 4)
> > >
> > >
> > > guqi wrote:
> > > > 给个简单一点的例子.
> > > >
> > > > ----- Original Message -----
> > > > From: "Qiangning Hong" <hongqn at gmail.com>
> > > > To: <python-chinese at lists.python.cn>
> > > > Sent: Friday, July 08, 2005 5:43 PM
> > > > Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> > > >
> > > >
> > > >
> > > >>On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
> > > >>
> > > >>>
> > > >>>例如:
> > > >>>定义时func(a,b,c)
> > > >>>
> > > >>>调用时func(a)或func(a,b)呢.
> > > >>
> > > >>用缺省参数或者不定长参数列表
> > > >>请看python自带的教程里4.7节"More on Defining Functions"
> > > >>http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000
> > > >>
> > > >>--
> > > >>Qiangning Hong
> > > >>
> > > >>I'm usually annoyed by IDEs because, for instance, they don't use VIM
> > > >>as an editor. Since I'm hooked to that, all IDEs I've used so far have
> > > >>failed to impress me.
> > > >>   -- Sybren Stuvel @ c.l.python
> > > >>
> > > >>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> > > >>
> > > >
> > > >
> > > > --------------------------------------------------------------------------------
> > > >
> > > >
> > > >
> > > >>_______________________________________________
> > > >>python-chinese list
> > > >>python-chinese at lists.python.cn
> > > >>http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > > --
> > > Qiangning
> > > _______________________________________________
> > > python-chinese list
> > > python-chinese at lists.python.cn
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> > --
> > 梅劲松
> >
> 
> 
> --------------------------------------------------------------------------------
> 
> 
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> > 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 


-- 
I like python! 
My Donews Blog: http://www.donews.net/limodou
New Google Maillist: http://groups-beta.google.com/group/python-cn

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月11日 星期一 11:21

guqi guqi at oasisnet.com.cn
Mon Jul 11 11:21:54 HKT 2005

给个例子先..

----- Original Message ----- 
From: "limodou" <limodou at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Monday, July 11, 2005 10:56 AM
Subject: Re: [python-chinese] 如何写出具有可变参数的函数


> *args是任意长的参数。
> 
> 在 05-7-11,guqi<guqi at oasisnet.com.cn> 写道:
> > 有人提供用*args,这是如何使用的呢.
> > 
> > ----- Original Message -----
> > From: "梅劲松" <stephen.cn at gmail.com>
> > To: <python-chinese at lists.python.cn>
> > Sent: Monday, July 11, 2005 9:30 AM
> > Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> > 
> > 
> > > 建议使用
> > > 用缺省函数:
> > >
> > > def func(a, b=1, c=2):
> > >   print a, b, c
> > >
> > > func(5) 输出 5 1 2
> > > func(5, 3) 输出 5 3 2
> > > func(5, 3, 4) 输出 5 3 4
> > >
> > > 这样的做法。
> > >
> > > 在 05-7-11,Qiangning Hong<hongqn at gmail.com> 写道:
> > > > 用缺省函数:
> > > >
> > > > def func(a, b=1, c=2):
> > > >   print a, b, c
> > > >
> > > > func(5) 输出 5 1 2
> > > > func(5, 3) 输出 5 3 2
> > > > func(5, 3, 4) 输出 5 3 4
> > > >
> > > >
> > > > 用不定长参数列表
> > > >
> > > > def func(a, *args):
> > > >   print a, args
> > > >
> > > > func(5) 输出 5 ()
> > > > func(5, 3) 输出 5 (3,)
> > > > func(5, 3, 4) 输出 5 (3, 4)
> > > >
> > > >
> > > > guqi wrote:
> > > > > 给个简单一点的例子.
> > > > >
> > > > > ----- Original Message -----
> > > > > From: "Qiangning Hong" <hongqn at gmail.com>
> > > > > To: <python-chinese at lists.python.cn>
> > > > > Sent: Friday, July 08, 2005 5:43 PM
> > > > > Subject: Re: [python-chinese] 如何写出具有可变参数的函数
> > > > >
> > > > >
> > > > >
> > > > >>On 7/8/05, guqi <guqi at oasisnet.com.cn> wrote:
> > > > >>
> > > > >>>
> > > > >>>例如:
> > > > >>>定义时func(a,b,c)
> > > > >>>
> > > > >>>调用时func(a)或func(a,b)呢.
> > > > >>
> > > > >>用缺省参数或者不定长参数列表
> > > > >>请看python自带的教程里4.7节"More on Defining Functions"
> > > > >>http://www.python.org/doc/2.4.1/tut/node6.html#SECTION006700000000000000000
> > > > >>
> > > > >>--
> > > > >>Qiangning Hong
> > > > >>
> > > > >>I'm usually annoyed by IDEs because, for instance, they don't use VIM
> > > > >>as an editor. Since I'm hooked to that, all IDEs I've used so far have
> > > > >>failed to impress me.
> > > > >>   -- Sybren Stuvel @ c.l.python
> > > > >>
> > > > >>Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
> > > > >>
> > > > >
> > > > >
> > > > > --------------------------------------------------------------------------------
> > > > >
> > > > >
> > > > >
> > > > >>_______________________________________________
> > > > >>python-chinese list
> > > > >>python-chinese at lists.python.cn
> > > > >>http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > > > --
> > > > Qiangning
> > > > _______________________________________________
> > > > python-chinese list
> > > > python-chinese at lists.python.cn
> > > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > >
> > >
> > > --
> > > 梅劲松
> > >
> > 
> > 
> > --------------------------------------------------------------------------------
> > 
> > 
> > > _______________________________________________
> > > python-chinese list
> > > python-chinese at lists.python.cn
> > > http://python.cn/mailman/listinfo/python-chinese
> > > 
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> > 
> > 
> > 
> 
> 
> -- 
> I like python! 
> My Donews Blog: http://www.donews.net/limodou
> New Google Maillist: http://groups-beta.google.com/group/python-cn
> 


--------------------------------------------------------------------------------


> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号