Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1798322
  • 博文数量: 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-12 23:57:51

2.5. Indenting Code

代码缩进

Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself.

Python 函数没有显示的begin &end,以及大括号{}来确定函数代码的开始和结束。唯一的分隔符就是冒号(:)以及代码本身的缩进。

Example 2.5. Indenting the buildConnectionString Function

buildConnectionString函数的缩进形式

  1. def buildConnectionString(params):
  2.     """Build a connection string from a dictionary of parameters.
  3.  
  4.     Returns string."""
  5.     return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

Code blocks are defined by their indentation. By "code block", I mean functions, if statements, for loops, while loops, and so forth. Indenting starts a block and unindenting ends it. There are no explicit braces, brackets, or keywords. This means that whitespace is significant, and must be consistent. In this example, the function code (including thedoc string) is indented four spaces. It doesn't need to be four spaces, it just needs to be consistent. The first line that is not indented is outside the function.

代码块被限制在缩进之内。我的意思是说函数,if语句,for 循环,while循环通过代码块来进行缩进。缩进代表了代码块的开始,而无缩进则表明代码块的结束。在Python中没有显示的大括号,小括号以及关键字来标识代码块。这同时也意味着,空白是有意义的,而且自始至终都必须一致。在上面的例子中,函数代码(包括函数注释)都是缩进4个空格。不一定非得是4个空格,但是你的风格必须一致。函数外边的第一行不需要要缩进。

Example 2.6, “if Statements” shows an example of code indentation with if statements.

2.6 if语句展现了一个if的所进形式。

Example 2.6. if Statements

 

  1. def fib(n):
  2.     print 'n =', n
  3.     if n > 1:
  4.         return n * fib(n - 1)
  5.     else:
  6.         print 'end of the line'
  7.         return 1


1

This is a function named fib that takes one argument, n. All the code within the function is indented.

该函数名为fib,有一个参数,n.函数内的所有代码都是缩进的。

2

Printing to the screen is very easy in Python, just use printprint statements can take any data type, including strings, integers, and other native types like dictionaries and lists that you'll learn about in the next chapter. You can even mix and match to print several things on one line by using a comma-separated list of values. Each value is printed on the same line, separated by spaces (the commas don't print). So when fib is called with 5, this will print "n = 5".

Python中,打印到屏幕是非常简单的,使用print就可以。Print语句的参数可以为任意数据类型,包括字符串,整型,以及其他原生类型如将在下章将要学习到的字典和列表。通过使用逗号来分隔的变量列表,你可以同时打印混合以及匹配不同类型的变量。所有的值都打印在同一行,通过空格来分隔(逗号本身不打印),当使用参数为5的值来调用函数fib时,将打印 “n = 5 “。

3

if statements are a type of code block. If the if expression evaluates to true, the indented block is executed, otherwise it falls to the else block.

If 语句是一个代码块。如果if语句的表达式为真,缩进代码将被执行,否则else代码块会被执行。

4

Of course if and else blocks can contain multiple lines, as long as they are all indented the same amount. This else block has two lines of code in it. There is no other special syntax for multi-line code blocks. Just indent and get on with your life.

当然,ifelse代码块可以包含多行代码,只要这些代码的缩进相同。上面的else代码块包含有两行代码。多行代码块没有其他的特殊的语法规则。仅仅使用缩进,你就可以开始你的新生活了。

After some initial protests and several snide analogies to Fortran, you will make peace with this and start seeing its benefits. One major benefit is that all Python programs look similar, since indentation is a language requirement and not a matter of style. This makes it easier to read and understand other people's Python code.

最初对这种缩进持保护态度,以及用心险恶同类比fortran后,你会平和的看待这种缩进,并能从中获益。其中最大的好处就是所有的Python程序看起来都是类似的,因为缩进是一种语言规定而不是编程风格。这使得阅读并理解别人的Python代码更加容易。

 

Python uses carriage returns to separate statements and a colon and indentation to separate code blocks. C++ and Java use semicolons to separate statements and curly braces to separate code blocks.Python使用回车来分隔语句,冒号和缩进来分隔代码块。C++Java使用分号来分隔语句,大括号来分隔代码块。

 

 

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