Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1754292
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-06-15 16:34:57

4.2. Using Optional and Named Arguments

可选和命名参数的使用

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Futhermore, arguments can be specified in any order by using named arguments. Stored procedures in SQL Server Transact/SQL can do this, so if you're a SQL Server scripting guru, you can skim this part.

Python允许函数参数拥有默认值;当函数被调用且不带参数是,参数就使用默认值。进一步而言,如果使用命名参数,参数可以以任意顺序来说明。SQL服务器中的Transact/SQL的存储过程也能这样,如果你是一个SQL Server 端脚本大师,你可以略过这部分。

Here is an example of info, a function with two optional arguments:

下面是一个带有两个可选参数的info函数示例。

 

  1. def info(object, spacing=10, collapse=1):

spacing and collapse are optional, because they have default values defined. object is required, because it has no default value. If info is called with only one argument, spacingdefaults to 10 and collapse defaults to 1. If info is called with two arguments, collapse still defaults to 1.

Spacing collapse 是可选的,因为它们在定义的时候是有默认值的。Object则是必须的,因为它没有默认值。假如函数Info在调用的时候只有一个参数,spacing的默认值为10,collapse的默认值为1.如果info在调用的时候带有两个参数,那么collapse 的值仍为1.

Say you want to specify a value for collapse but want to accept the default value for spacing. In most languages, you would be out of luck, because you would need to call the function with three arguments. But in Python, arguments can be specified by name, in any order.

如果你想给collpase 传值,那是想让spacing接受默认值,那应该怎么办呢?在大多数语言中,你可能非常不幸,因为你在调用这个函数的时候必须使用三个参数。但是在Python中,参数可以以任意顺序使用变量名来确定。

Example 4.4. Valid Calls of info

4.4 Info函数的合法调用

  1. info(odbchelper)
  2. info(odbchelper, 12)
  3. info(odbchelper, collapse=0)
  4. info(spacing=15, object=odbchelper)

1

With only one argument, spacing gets its default value of 10 and collapse gets its default value of 1.

只有一个参数,其它两个参数都是用默认值。

2

With two arguments, collapse gets its default value of 1.

使用带有两个参数的函数来调用,最后一个参数使用默认值

3

Here you are naming the collapse argument explicitly and specifying its value. spacing still gets its default value of 10.

这儿显示的命名collpse参数并赋予值,spacing仍然使用默认值。

4

Even required arguments (like object, which has no default value) can be named, and named arguments can appear in any order.

即使必须的参数(如object ,它没有默认值)可以通过命名的方式使用,而命名的参数在调用的时候顺序任意。

This looks totally whacked until you realize that arguments are simply a dictionary. The “normal” method of calling functions without argument names is actually just a shorthand where Python matches up the values with the argument names in the order they're specified in the function declaration. And most of the time, you'll call functions the “normal” way, but you always have the additional flexibility if you need it.

在你没有意识到形参仅是简单的字典前,这可能会使你疲惫不堪。不带参数名的调用函数的方法是通常方法,这只是一种快速的方法。这其中Python遵循函数声明,按顺序匹配值来和参数名对应。在大多数情况,你以通常的方式调用函数,但是如果你需要,你总可以使用其它的灵活方法。

Note

 

The only thing you need to do to call a function is specify a value (somehow) for each required argument; the manner and order in which you do that is up to you.

在调用函数的时候,你唯一需要做的就是以某种方式确定必须的参数。调用的方式和顺序你自己决定。

 

 

阅读(841) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~