summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xzh-cn/git-cn.html.markdown2
-rwxr-xr-xzh-cn/haskell-cn.html.markdown407
-rwxr-xr-xzh-cn/python-cn.html.markdown475
3 files changed, 883 insertions, 1 deletions
diff --git a/zh-cn/git-cn.html.markdown b/zh-cn/git-cn.html.markdown
index 7aab8986..8c24f0b8 100755
--- a/zh-cn/git-cn.html.markdown
+++ b/zh-cn/git-cn.html.markdown
@@ -120,7 +120,7 @@ $ git help
$ git help -a
# 在文档当中查找特定的命令
-$ git help <命令>
+# git help <命令>
$ git help add
$ git help commit
$ git help init
diff --git a/zh-cn/haskell-cn.html.markdown b/zh-cn/haskell-cn.html.markdown
new file mode 100755
index 00000000..cb0de467
--- /dev/null
+++ b/zh-cn/haskell-cn.html.markdown
@@ -0,0 +1,407 @@
+---
+language: haskell
+filename: learn-haskell.hs
+contributors:
+ - ["Adit Bhargava", "http://adit.io"]
+translators:
+ - ["Peiyong Lin", ""]
+lang: zh-cn
+---
+
+Haskell 被设计成一种实用的纯函数式编程语言。它因为 monads 及其类型系统而出名,但是我回归到它本身因为。Haskell 使得编程对于我而言是一种真正的快乐。
+
+```haskell
+-- 单行注释以两个破折号开头
+{- 多行注释像这样
+ 被一个闭合的块包围
+-}
+
+----------------------------------------------------
+-- 1. 简单的数据类型和操作符
+----------------------------------------------------
+
+-- 你有数字
+3 -- 3
+-- 数学计算就像你所期待的那样
+1 + 1 -- 2
+8 - 1 -- 7
+10 * 2 -- 20
+35 / 5 -- 7.0
+
+-- 默认除法不是整除
+35 / 4 -- 8.75
+
+-- 整除
+35 `div` 4 -- 8
+
+-- 布尔值也简单
+True
+False
+
+-- 布尔操作
+not True -- False
+not False -- True
+1 == 1 -- True
+1 /= 1 -- False
+1 < 10 -- True
+
+-- 在上述的例子中,`not` 是一个接受一个值的函数。
+-- Haskell 不需要括号来调用函数。。。所有的参数
+-- 都只是在函数名之后列出来。因此,通常的函数调用模式是:
+-- func arg1 arg2 arg3...
+-- 查看关于函数的章节以获得如何写你自己的函数的相关信息。
+
+-- 字符串和字符
+"This is a string."
+'a' -- 字符
+'对于字符串你不能使用单引号。' -- 错误!
+
+-- 连结字符串
+"Hello " ++ "world!" -- "Hello world!"
+
+-- 一个字符串是一系列字符
+"This is a string" !! 0 -- 'T'
+
+
+----------------------------------------------------
+-- 列表和元组
+----------------------------------------------------
+
+-- 一个列表中的每一个元素都必须是相同的类型
+-- 下面两个列表一样
+[1, 2, 3, 4, 5]
+[1..5]
+
+-- 在 Haskell 你可以拥有含有无限元素的列表
+[1..] -- 一个含有所有自然数的列表
+
+-- 因为 Haskell 有“懒惰计算”,所以无限元素的列表可以正常运作。这意味着
+-- Haskell 可以只在它需要的时候计算。所以你可以请求
+-- 列表中的第1000个元素,Haskell 会返回给你
+
+[1..] !! 999 -- 1000
+
+-- Haskell 计算了列表中 1 - 1000 个元素。。。但是
+-- 这个无限元素的列表中剩下的元素还不存在! Haskell 不会
+-- 真正地计算它们知道它需要。
+
+<FS>- 连接两个列表
+[1..5] ++ [6..10]
+
+-- 往列表头增加元素
+0:[1..5] -- [0, 1, 2, 3, 4, 5]
+
+-- 列表中的下标
+[0..] !! 5 -- 5
+
+-- 更多列表操作
+head [1..5] -- 1
+tail [1..5] -- [2, 3, 4, 5]
+init [1..5] -- [1, 2, 3, 4]
+last [1..5] -- 5
+
+-- 列表推导
+[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10]
+
+-- 附带条件
+[x*2 | x <-[1..5], x*2 > 4] -- [6, 8, 10]
+
+-- 元组中的每一个元素可以是不同类型的,但是一个元组
+-- 的长度是固定的
+-- 一个元组
+("haskell", 1)
+
+-- 获取元组中的元素
+fst ("haskell", 1) -- "haskell"
+snd ("haskell", 1) -- 1
+
+----------------------------------------------------
+-- 3. 函数
+----------------------------------------------------
+-- 一个接受两个变量的简单函数
+add a b = a + b
+
+-- 注意,如果你使用 ghci (Hakell 解释器)
+-- 你将需要使用 `let`,也就是
+-- let add a b = a + b
+
+-- 使用函数
+add 1 2 -- 3
+
+-- 你也可以把函数放置在两个参数之间
+-- 附带倒引号:
+1 `add` 2 -- 3
+
+-- 你也可以定义不带字符的函数!这使得
+-- 你定义自己的操作符!这里有一个操作符
+-- 来做整除
+(//) a b = a `div` b
+35 // 4 -- 8
+
+-- 守卫:一个简单的方法在函数里做分支
+fib x
+ | x < 2 = x
+ | otherwise = fib (x - 1) + fib (x - 2)
+
+-- 模式匹配是类型的。这里有三种不同的 fib
+-- 定义。Haskell 将自动调用第一个
+-- 匹配值的模式的函数。
+fib 1 = 1
+fib 2 = 2
+fib x = fib (x - 1) + fib (x - 2)
+
+-- 元组的模式匹配:
+foo (x, y) = (x + 1, y + 2)
+
+-- 列表的模式匹配。这里 `x` 是列表中第一个元素,
+-- 并且 `xs` 是列表剩余的部分。我们可以写
+-- 自己的 map 函数:
+myMap func [] = []
+myMap func (x:xs) = func x:(myMap func xs)
+
+-- 编写出来的匿名函数带有一个反斜杠,后面跟着
+-- 所有的参数。
+myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
+
+-- 使用 fold (在一些语言称为`inject`)随着一个匿名的
+-- 函数。foldl1 意味着左折叠(fold left), 并且使用列表中第一个值
+-- 作为累加器的初始化值。
+foldl1 (\acc x -> acc + x) [1..5] -- 15
+
+----------------------------------------------------
+-- 4. 更多的函数
+----------------------------------------------------
+
+-- 柯里化(currying):如果你不传递函数中所有的参数,
+-- 它就变成“柯里化的”。这意味着,它返回一个接受剩余参数的函数。
+
+add a b = a + b
+foo = add 10 -- foo 现在是一个接受一个数并对其加 10 的函数
+foo 5 -- 15
+
+-- 另外一种方式去做同样的事
+foo = (+10)
+foo 5 -- 15
+
+-- 函数组合
+-- (.) 函数把其它函数链接到一起
+-- 举个列子,这里 foo 是一个接受一个值的函数。它对接受的值加 10,
+-- 并对结果乘以 5,之后返回最后的值。
+foo = (*5) . (+10)
+
+-- (5 + 10) * 5 = 75
+foo 5 -- 75
+
+-- 修复优先级
+-- Haskell 有另外一个函数称为 `$`。它改变优先级
+-- 使得其左侧的每一个操作先计算然后应用到
+-- 右侧的每一个操作。你可以使用 `.` 和 `$` 来除去很多
+-- 括号:
+
+-- before
+(even (fib 7)) -- true
+
+-- after
+even . fib $ 7 -- true
+
+----------------------------------------------------
+-- 5. 类型签名
+----------------------------------------------------
+
+-- Haskell 有一个非常强壮的类型系统,一切都有一个类型签名。
+
+-- 一些基本的类型:
+5 :: Integer
+"hello" :: String
+True :: Bool
+
+-- 函数也有类型。
+-- `not` 接受一个布尔型返回一个布尔型:
+-- not :: Bool -> Bool
+
+-- 这是接受两个参数的函数:
+-- add :: Integer -> Integer -> Integer
+
+-- 当你定义一个值,在其上写明它的类型是一个好实践:
+double :: Integer -> Integer
+double x = x * 2
+
+----------------------------------------------------
+-- 6. 控制流和 If 语句
+----------------------------------------------------
+
+-- if 语句
+haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"
+
+-- if 语句也可以有多行,缩进是很重要的
+haskell = if 1 == 1
+ then "awesome"
+ else "awful"
+
+-- case 语句:这里是你可以怎样去解析命令行参数
+case args of
+ "help" -> printHelp
+ "start" -> startProgram
+ _ -> putStrLn "bad args"
+
+-- Haskell 没有循环因为它使用递归取代之。
+-- map 应用一个函数到一个数组中的每一个元素
+
+map (*2) [1..5] -- [2, 4, 6, 8, 10]
+
+-- 你可以使用 map 来编写 for 函数
+for array func = map func array
+
+-- 然后使用它
+for [0..5] $ \i -> show i
+
+-- 我们也可以像这样写:
+for [0..5] show
+
+-- 你可以使用 foldl 或者 foldr 来分解列表
+-- foldl <fn> <initial value> <list>
+foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
+
+-- 这和下面是一样的
+(2 * (2 * (2 * 4 + 1) + 2) + 3)
+
+-- foldl 是左手边的,foldr 是右手边的-
+foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16
+
+-- 这和下面是一样的
+(2 * 3 + (2 * 2 + (2 * 1 + 4)))
+
+----------------------------------------------------
+-- 7. 数据类型
+----------------------------------------------------
+
+-- 这里展示在 Haskell 中你怎样编写自己的数据类型
+
+data Color = Red | Blue | Green
+
+-- 现在你可以在函数中使用它:
+
+
+say :: Color -> String
+say Red = "You are Red!"
+say Blue = "You are Blue!"
+say Green = "You are Green!"
+
+-- 你的数据类型也可以有参数:
+
+data Maybe a = Nothing | Just a
+
+-- 类型 Maybe 的所有
+Just "hello" -- of type `Maybe String`
+Just 1 -- of type `Maybe Int`
+Nothing -- of type `Maybe a` for any `a`
+
+----------------------------------------------------
+-- 8. Haskell IO
+----------------------------------------------------
+
+-- 虽然在没有解释 monads 的情况下 IO不能被完全地解释,
+-- 着手解释到位并不难。
+
+-- 当一个 Haskell 程序被执行,函数 `main` 就被调用。
+-- 它必须返回一个类型 `IO ()` 的值。举个列子:
+
+main :: IO ()
+main = putStrLn $ "Hello, sky! " ++ (say Blue)
+-- putStrLn has type String -> IO ()
+
+-- 如果你能实现你的程序依照函数从 String 到 String,那样编写 IO 是最简单的。
+-- 函数
+-- interact :: (String -> String) -> IO ()
+-- 输入一些文本,在其上运行一个函数,并打印出输出
+
+countLines :: String -> String
+countLines = show . length . lines
+
+main' = interact countLines
+
+-- 你可以考虑一个 `IO()` 类型的值,当做一系列计算机所完成的动作的代表,
+-- 就像一个以命令式语言编写的计算机程序。我们可以使用 `do` 符号来把动作链接到一起。
+-- 举个列子:
+
+sayHello :: IO ()
+sayHello = do
+ putStrLn "What is your name?"
+ name <- getLine -- this gets a line and gives it the name "input"
+ putStrLn $ "Hello, " ++ name
+
+-- 练习:编写只读取一行输入的 `interact`
+
+-- 然而,`sayHello` 中的代码将不会被执行。唯一被执行的动作是 `main` 的值。
+-- 为了运行 `sayHello`,注释上面 `main` 的定义,并代替它:
+-- main = sayHello
+
+-- 让我们来更好地理解刚才所使用的函数 `getLine` 是怎样工作的。它的类型是:
+-- getLine :: IO String
+-- 你可以考虑一个 `IO a` 类型的值,代表一个当被执行的时候
+-- 将产生一个 `a` 类型的值的计算机程序(除了它所做的任何事之外)。我们可以保存和重用这个值通过 `<-`。
+-- 我们也可以写自己的 `IO String` 类型的动作:
+
+action :: IO String
+action = do
+ putStrLn "This is a line. Duh"
+ input1 <- getLine
+ input2 <- getLine
+ -- The type of the `do` statement is that of its last line.
+ -- `return` is not a keyword, but merely a function
+ return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
+
+-- 我们可以使用这个动作就像我们使用 `getLine`:
+
+main'' = do
+ putStrLn "I will echo two lines!"
+ result <- action
+ putStrLn result
+ putStrLn "This was all, folks!"
+
+-- `IO` 类型是一个 "monad" 的例子。Haskell 使用一个 monad 来做 IO的方式允许它是一门纯函数式语言。
+-- 任何与外界交互的函数(也就是 IO) 都在它的类型签名处做一个 `IO` 标志
+-- 着让我们推出 什么样的函数是“纯洁的”(不与外界交互,不修改状态) 和 什么样的函数不是 “纯洁的”
+
+-- 这是一个强有力的特征,因为并发地运行纯函数是简单的;因此,Haskell 中并发是非常简单的。
+
+
+----------------------------------------------------
+-- 9. The Haskell REPL
+----------------------------------------------------
+
+-- 键入 `ghci` 开始 repl。
+-- 现在你可以键入 Haskell 代码。
+-- 任何新值都需要通过 `let` 来创建:
+
+let foo = 5
+
+-- 你可以查看任何值的类型,通过命令 `:t`:
+
+>:t foo
+foo :: Integer
+
+-- 你也可以运行任何 `IO ()`类型的动作
+
+> sayHello
+What is your name?
+Friend!
+Hello, Friend!
+
+```
+
+还有很多关于 Haskell,包括类型类和 monads。这些是使得编码 Haskell 是如此有趣的主意。我用一个最后的 Haskell 例子来结束:一个 Haskell 的快排实现:
+
+```haskell
+qsort [] = []
+qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater
+ where lesser = filter (< p) xs
+ greater = filter (>= p) xs
+```
+
+安装 Haskell 是简单的。你可以从[这里](http://www.haskell.org/platform/)获得它。
+
+你可以从优秀的
+[Learn you a Haskell](http://learnyouahaskell.com/) 或者
+[Real World Haskell](http://book.realworldhaskell.org/)
+找到优雅不少的入门介绍。 \ No newline at end of file
diff --git a/zh-cn/python-cn.html.markdown b/zh-cn/python-cn.html.markdown
new file mode 100755
index 00000000..259e4ed8
--- /dev/null
+++ b/zh-cn/python-cn.html.markdown
@@ -0,0 +1,475 @@
+---
+language: python
+contributors:
+ - ["Louie Dinh", "http://ldinh.ca"]
+translators:
+ - ["Chenbo Li", "http://binarythink.net"]
+filename: learnpython.py
+lang: zh-cn
+---
+
+Python 由 Guido Van Rossum 在90年代初创建。 它现在是最流行的语言之一
+我喜爱python是因为它有极为清晰的语法,甚至可以说,它就是可以执行的伪代码
+
+很欢迎来自您的反馈,你可以在[@louiedinh](http://twitter.com/louiedinh) 和 louiedinh [at] [google's email service] 这里找到我
+
+注意: 这篇文章针对的版本是Python 2.7,但大多也可使用于其他Python 2的版本
+如果是Python 3,请在网络上寻找其他教程
+
+```python
+# 单行注释
+""" 多行字符串可以用
+ 三个引号包裹,不过这也可以被当做
+ 多行注释
+"""
+
+####################################################
+## 1. 原始数据类型和操作符
+####################################################
+
+# 数字类型
+3 #=> 3
+
+# 简单的算数
+1 + 1 #=> 2
+8 - 1 #=> 7
+10 * 2 #=> 20
+35 / 5 #=> 7
+
+# 整数的除法会自动取整
+5 / 2 #=> 2
+
+# 要做精确的除法,我们需要引入浮点数
+2.0 # 浮点数
+11.0 / 4.0 #=> 2.75 好多了
+
+# 括号具有最高优先级
+(1 + 3) * 2 #=> 8
+
+# 布尔值也是原始数据类型
+True
+False
+
+# 用not来取非
+not True #=> False
+not False #=> True
+
+# 相等
+1 == 1 #=> True
+2 == 1 #=> False
+
+# 不等
+1 != 1 #=> False
+2 != 1 #=> True
+
+# 更多的比较操作符
+1 < 10 #=> True
+1 > 10 #=> False
+2 <= 2 #=> True
+2 >= 2 #=> True
+
+# 比较运算可以连起来写!
+1 < 2 < 3 #=> True
+2 < 3 < 2 #=> False
+
+# 字符串通过"或'括起来
+"This is a string."
+'This is also a string.'
+
+# 字符串通过加号拼接
+"Hello " + "world!" #=> "Hello world!"
+
+# 字符串可以被视为字符的列表
+"This is a string"[0] #=> 'T'
+
+# % 可以用来格式化字符串
+"%s can be %s" % ("strings", "interpolated")
+
+# 也可以用format方法来格式化字符串
+# 推荐使用这个方法
+"{0} can be {1}".format("strings", "formatted")
+# 也可以用变量名代替数字
+"{name} wants to eat {food}".format(name="Bob", food="lasagna")
+
+# None 是对象
+None #=> None
+
+# 不要用相等 `==` 符号来和None进行比较
+# 要用 `is`
+"etc" is None #=> False
+None is None #=> True
+
+# 'is' 可以用来比较对象的相等性
+# 这个操作符在比较原始数据时没多少用,但是比较对象时必不可少
+
+# None, 0, 和空字符串都被算作False
+# 其他的均为True
+0 == False #=> True
+"" == False #=> True
+
+
+####################################################
+## 2. 变量和集合
+####################################################
+
+# 很方便的输出
+print "I'm Python. Nice to meet you!"
+
+
+# 给变量赋值前不需要事先生命
+some_var = 5 # 规范用小写字母和下划线来做为变量名
+some_var #=> 5
+
+# 访问之前为赋值的变量会抛出异常
+# 查看控制流程一节来了解异常处理
+some_other_var # 抛出命名异常
+
+# if语句可以作为表达式来使用
+"yahoo!" if 3 > 2 else 2 #=> "yahoo!"
+
+# 列表用来保存序列
+li = []
+# 可以直接初始化列表
+other_li = [4, 5, 6]
+
+# 在列表末尾添加元素
+li.append(1) #li 现在是 [1]
+li.append(2) #li 现在是 [1, 2]
+li.append(4) #li 现在是 [1, 2, 4]
+li.append(3) #li 现在是 [1, 2, 4, 3]
+# 移除列表末尾元素
+li.pop() #=> 3 and li is now [1, 2, 4]
+# 放回来
+li.append(3) # li is now [1, 2, 4, 3] again.
+
+# 像其他语言访问数组一样访问列表
+li[0] #=> 1
+# 访问最后一个元素
+li[-1] #=> 3
+
+# 越界会抛出异常
+li[4] # 抛出越界异常
+
+# 切片语法需要用到列表的索引访问
+# 可以看做数学之中左闭右开区间
+li[1:3] #=> [2, 4]
+# 省略开头的元素
+li[2:] #=> [4, 3]
+# 省略末尾的元素
+li[:3] #=> [1, 2, 4]
+
+# 删除特定元素
+del li[2] # li 现在是 [1, 2, 3]
+
+# 合并列表
+li + other_li #=> [1, 2, 3, 4, 5, 6] - 不改变这两个列表
+
+# 通过拼接合并列表
+li.extend(other_li) # li 是 [1, 2, 3, 4, 5, 6]
+
+# 用in来返回元素是否在列表中
+1 in li #=> True
+
+# 返回列表长度
+len(li) #=> 6
+
+
+# 元组类似于列表,但是他是不可改变的
+tup = (1, 2, 3)
+tup[0] #=> 1
+tup[0] = 3 # 类型错误
+
+# 对于大多数的列表操作,也适用于元组
+len(tup) #=> 3
+tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
+tup[:2] #=> (1, 2)
+2 in tup #=> True
+
+# 你可以将元组解包赋给多个变量
+a, b, c = (1, 2, 3) # a是1,b是2,c是3
+# 如果不加括号,那么会自动视为元组
+d, e, f = 4, 5, 6
+# 现在我们可以看看交换两个数字是多么容易的事
+e, d = d, e # d是5,e是4
+
+
+# 字典用来储存映射关系
+empty_dict = {}
+# 字典初始化
+filled_dict = {"one": 1, "two": 2, "three": 3}
+
+# 字典也用中括号访问元素
+filled_dict["one"] #=> 1
+
+# 把所有的键保存在列表中
+filled_dict.keys() #=> ["three", "two", "one"]
+# 键的顺序并不是唯一的,得到的不一定是这个顺序
+
+# 把所有的值保存在列表中
+filled_dict.values() #=> [3, 2, 1]
+# 和键的顺序相同
+
+# 判断一个键是否存在
+"one" in filled_dict #=> True
+1 in filled_dict #=> False
+
+# 查询一个不存在的键会抛出键异常
+filled_dict["four"] # 键异常
+
+# 用get方法来避免键异常
+filled_dict.get("one") #=> 1
+filled_dict.get("four") #=> None
+# get方法支持在不存在的时候返回一个默认值
+filled_dict.get("one", 4) #=> 1
+filled_dict.get("four", 4) #=> 4
+
+# Setdefault是一个更安全的添加字典元素的方法
+filled_dict.setdefault("five", 5) #filled_dict["five"] 的值为 5
+filled_dict.setdefault("five", 6) #filled_dict["five"] 的值仍然是 5
+
+
+# 集合储存无顺序的元素
+empty_set = set()
+# 出事话一个集合
+some_set = set([1,2,2,3,4]) # filled_set 现在是 set([1, 2, 3, 4])
+
+# Python 2.7 之后,大括号可以用来表示集合
+filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}
+
+# 为集合添加元素
+filled_set.add(5) # filled_set 现在是 {1, 2, 3, 4, 5}
+
+# 用&来实现集合的交
+other_set = {3, 4, 5, 6}
+filled_set & other_set #=> {3, 4, 5}
+
+# 用|来实现集合的并
+filled_set | other_set #=> {1, 2, 3, 4, 5, 6}
+
+# 用-来实现集合的差
+{1,2,3,4} - {2,3,5} #=> {1, 4}
+
+# 用in来判断元素是否存在于集合中
+2 in filled_set #=> True
+10 in filled_set #=> False
+
+
+####################################################
+## 3. 控制流程
+####################################################
+
+# 新建一个变量
+some_var = 5
+
+# 这是个if语句,在python中缩进是很重要的。
+# 会输出 "some var is smaller than 10"
+if some_var > 10:
+ print "some_var is totally bigger than 10."
+elif some_var < 10: # 这个 elif 语句是不必须的
+ print "some_var is smaller than 10."
+else: # 也不是必须的
+ print "some_var is indeed 10."
+
+
+"""
+用for循环遍历列表
+输出:
+ dog is a mammal
+ cat is a mammal
+ mouse is a mammal
+"""
+for animal in ["dog", "cat", "mouse"]:
+ # 你可以用 % 来格式化字符串
+ print "%s is a mammal" % animal
+
+"""
+`range(number)` 返回从0到给定数字的列表
+输出:
+ 0
+ 1
+ 2
+ 3
+"""
+for i in range(4):
+ print i
+
+"""
+While循环
+输出:
+ 0
+ 1
+ 2
+ 3
+"""
+x = 0
+while x < 4:
+ print x
+ x += 1 # Shorthand for x = x + 1
+
+# 用 try/except块来处理异常
+
+# Python 2.6 及以上适用:
+try:
+ # 用raise来抛出异常
+ raise IndexError("This is an index error")
+except IndexError as e:
+ pass # Pass就是什么都不做,不过通常这里会做一些恢复工作
+
+
+####################################################
+## 4. 函数
+####################################################
+
+# 用def来新建函数
+def add(x, y):
+ print "x is %s and y is %s" % (x, y)
+ return x + y # Return values with a return statement
+
+# 调用带参数的函数
+add(5, 6) #=> 输出 "x is 5 and y is 6" 返回 11
+
+# 通过关键字赋值来调用函数
+add(y=6, x=5) # 顺序是无所谓的
+
+# 我们也可以定义接受多个变量的函数,这些变量是按照顺序排列的
+def varargs(*args):
+ return args
+
+varargs(1, 2, 3) #=> (1,2,3)
+
+
+# 我们也可以定义接受多个变量的函数,这些变量是按照关键字排列的
+def keyword_args(**kwargs):
+ return kwargs
+
+# 实际效果:
+keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}
+
+# 你也可以同时将一个函数定义成两种形式
+def all_the_args(*args, **kwargs):
+ print args
+ print kwargs
+"""
+all_the_args(1, 2, a=3, b=4) prints:
+ (1, 2)
+ {"a": 3, "b": 4}
+"""
+
+# 当调用函数的时候,我们也可以和之前所做的相反,把元组和字典展开为参数
+args = (1, 2, 3, 4)
+kwargs = {"a": 3, "b": 4}
+all_the_args(*args) # equivalent to foo(1, 2, 3, 4)
+all_the_args(**kwargs) # equivalent to foo(a=3, b=4)
+all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4)
+
+# Python 有一等函数:
+def create_adder(x):
+ def adder(y):
+ return x + y
+ return adder
+
+add_10 = create_adder(10)
+add_10(3) #=> 13
+
+# 匿名函数
+(lambda x: x > 2)(3) #=> True
+
+# 内置高阶函数
+map(add_10, [1,2,3]) #=> [11, 12, 13]
+filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]
+
+# 可以用列表方法来对高阶函数进行更巧妙的引用
+[add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13]
+[x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7]
+
+####################################################
+## 5. 类
+####################################################
+
+# 我们新建的类是从object类中继承的
+class Human(object):
+
+ # 类属性,由所有类的对象共享
+ species = "H. sapiens"
+
+ # 基本构造函数
+ def __init__(self, name):
+ # 将参数赋给对象成员属性
+ self.name = name
+
+ # 成员方法,参数要有self
+ def say(self, msg):
+ return "%s: %s" % (self.name, msg)
+
+ # 类方法由所有类的对象共享
+ # 这类方法在调用时,会把类本身传给第一个参数
+ @classmethod
+ def get_species(cls):
+ return cls.species
+
+ # 静态方法是不需要类和对象的引用就可以调用的方法
+ @staticmethod
+ def grunt():
+ return "*grunt*"
+
+
+# 实例化一个类
+i = Human(name="Ian")
+print i.say("hi") # 输出 "Ian: hi"
+
+j = Human("Joel")
+print j.say("hello") # 输出 "Joel: hello"
+
+# 访问类的方法
+i.get_species() #=> "H. sapiens"
+
+# 改变共享属性
+Human.species = "H. neanderthalensis"
+i.get_species() #=> "H. neanderthalensis"
+j.get_species() #=> "H. neanderthalensis"
+
+# 访问静态变量
+Human.grunt() #=> "*grunt*"
+
+
+####################################################
+## 6. 模块
+####################################################
+
+# 我们可以导入其他模块
+import math
+print math.sqrt(16) #=> 4
+
+# 我们也可以从一个模块中特定的函数
+from math import ceil, floor
+print ceil(3.7) #=> 4.0
+print floor(3.7) #=> 3.0
+
+# 从模块中导入所有的函数
+# 警告:不推荐使用
+from math import *
+
+# 简写模块名
+import math as m
+math.sqrt(16) == m.sqrt(16) #=> True
+
+# Python的模块其实只是普通的python文件
+# 你也可以创建自己的模块,并且导入它们
+# 模块的名字就和文件的名字相同
+
+# 以可以通过下面的信息找找要成为模块需要什么属性或方法
+import math
+dir(math)
+
+
+```
+
+## 更多阅读
+
+希望学到更多?试试下面的链接:
+
+* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)
+* [Dive Into Python](http://www.diveintopython.net/)
+* [The Official Docs](http://docs.python.org/2.6/)
+* [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)
+* [Python Module of the Week](http://pymotw.com/2/)