函数式编程
Python语言教程手册(2)
上页
Tuples 元组是一个序列类型,就像List,但它是不可改变的。一个元组由数个逗号分隔的值。
>>> t = (1, 2)
>>> t
(1, 2)
>>> t[0]
1
>>> t[0] = 1.1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t = 1, 2
>>> t
(1, 2)
>>> del t[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> for item in t:
... print item
...
1
2
Sets
Sets 是包含唯一(不重复)元素的集合
>>> x = set([1, 2, 1])
>>> x
set([1, 2])
>>> x.add(3)
>>> x
set([1, 2, 3])
>>> x = {1, 3, 4, 1}
>>> x
set([1, 3, 4])
>>> 1 in x
True
>>> -1 in x
False
>>>
Again Lists
>>> even_numbers = []
>>> for number in range(0, 9):
... if number % 2 == 0:
... even_numbers.append(number)
...
>>> even_numbers
[0, 2, 4, 6, 8]
作为一个程序员,你的工作应该是更少代码:
List 内涵
>>> [x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [x + 1 for x in range(10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers = []
>>> for x in range(10):
... numbers.append(x + 1)
...
>>> print numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> even_numbers = [x for x in range(10) if x %2 == 0]
>>> even_numbers
[0, 2, 4, 6, 8]
>>> [(x, y) for x in range(5) for y in range(5) if (x+y)%2 == 0]
[(0, 0), (0, 2), (0, 4), (1, 1), (1, 3), (2, 0), (2, 2), (2, 4), (3, 1), (3, 3 ), (4, 0), (4, 2), (4, 4)]
>>>
Dictionaries字典
>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> d['a']
1
>>> d.get('a')
1
>>> d['z']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'z'
>>> d.get('z')
>>>
>>> d['a'] = 2
>>> d
{'a': 2, 'c': 3, 'b': 2}
>>> d['z'] = 26
>>> d
{'a': 2, 'c': 3, 'b': 2, 'z': 26}
>>> d.keys()
['a', 'c', 'b', 'z']
>>> d.values()
[2, 3, 2, 26]
>>> d.items()
[('a', 2), ('c', 3), ('b', 2), ('z', 26)]
>>> type(d.items())
<type 'list'>
>>> d = {'a': 2, 'b': 2, 'c': 3, 'z': 26}
>>> for key in d:
... print key
...
a
c
b
z
>>> for key, value in d.items():
... print key, value
...
a 2
c 3
b 2
z 26
>>> 'a' in d
True
>>> d.has_key('a')
True
Function函数
如同一个值能用一个名称相关联,一段逻辑也可以用一个带名称的函数表达
>>> def square(x):
... return x * x
...
>>> square(2)
4
>>> square(2+1)
9
>>> square(x=5)
25
>>> def dont_return(name):
... print "Master %s ordered not to return value" % name
...
>>> dont_return("Python")
Master Python ordered not to return value
>>> def power(base, to_raise=2):
... return base ** to_raise
...
>>> power(3)
9
>>> power(3, 3)
27
>>> def power(to_raise=2, base):
... return base ** to_raise
...
File "<stdin>", line 1
SyntaxError: non-default argument follows default argument
>>> square(3) + square(4)
25
>>> power(base=square(2))
16
>>> def sum_of_square(x, y):
... return square(x) + square(y)
...
>>> sum_of_square(2, 3)
13
>>> s = square
>>> s(4)
16
>>> def fxy(f, x, y):
... return f(x) + f(y)
...
>>> fxy(square, 3, 4)
25
Methods - 方法是对象中的特殊函数
>>> lang = "Python"
>>> type(lang)
<type 'str'>
>>> dir(lang)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser',
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum',
'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition',
'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> lang.upper()
'PYTHON'
>>> help(lang.upper)
>>> lang.startswith('P')
True
>>> help(lang.startswith)
>>> lang.startswith('y', 1)
True
Files文件
>>> f = open('foo.txt', 'w')
>>> help(f)
>>> f.write("First line")
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.readline()
'First line'
>>> f.readline()
''
>>> f = open('foo.txt', 'a')
>>> f.write('Second line')
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.readline()
'First lineSecond line'
>>> f = open('foo.txt', 'a')
>>> f.write("New line\n")
>>> f.write("One more new line")
>>> f.close()
>>> f = open('foo.txt', 'r')
>>> f.readline()
'First lineSecond lineNew line\n'
>>> f.readline()
'One more new line'
>>> f.readline()
''
>>> f.close()
>>> f = open('foo.txt')
>>> f.readlines()
['First lineSecond lineNew line\n', 'One more new line']
>>> f = open('foo.txt', 'w')
>>> f.writelines(["1\n", "2\n"])
>>> f.close()
>>> f.readlines()
>>> f = open('foo.txt')
>>> f.readlines()
['1\n', '2\n']
>>> f.close()
Exception Handling出错处理
>>> f = open('a.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'a.txt'
>>> try:
... f = open('a.txt')
... except:
... print "Exception occured"
...
Exception occured
>>> try:
... f = open('a.txt')
... except IOError, e:
... print e.message
...
>>> e
IOError(2, 'No such file or directory')
>>> dir(e)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', 'args', 'errno', 'filename', 'message', 'strerror']
>>> e.strerror
'No such file or directory'
>>> try:
... print l[4]
... except IndexError, e:
... print e
...
list index out of range
>>> raise Exception("error message")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: error message
>>> try:
... print "a"
... raise Exception("doom")
... except:
... print "b"
... else:
... print "c"
... finally:
... print "d"
...
a
b
d
面向对象编程
>>> class BankAccount:
def __init__(self):
self.balance = 0
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
>>> a = BankAccount()
>>> b = BankAccount()
>>> a.deposit(200)
200
>>> b.deposit(500)
500
>>> a.withdraw(20)
180
>>> b.withdraw(1000)
-500
>>> class MinimumBalanceAccount(BankAccount):
... def __init__(self, minimum_balance):
... BankAccount.__init__(self)
... self.minimum_balance = minimum_balance
...
... def withdraw(self, amount):
... if self.balance - amount < self.minimum_balance:
... print "Sorry, you need to maintain minimum balance"
... else:
... return BankAccount.withdraw(self, amount)
>>> a = MinimumBalanceAccount(500)
>>> a
<__main__.MinimumBalanceAccount instance at 0x7fa0bf329878>
>>> a.deposit(2000)
2000
>>> a.withdraw(1000)
1000
>>> a.withdraw(1000)
Sorry, you need to maintain minimum balance
>>> class A:
... def f(self):
... return self.g()
... def g(self):
... return "A"
...
>>> a = A()
>>> a.f()
'A'
>>> a.g()
'A'
>>> class A:
... def __init__(self):
... self._protected = 1
... self.__private = 2
...
>>> a = A()
>>> a._protected
1
>>> a.__private
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute '__private'
Python案例程序
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class BankAccount:
def __init__(self):
self.balance = 0
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
class MinimumBalanceAccount(BankAccount):
def __init__(self, minimum_balance):
BankAccount.__init__(self)
self.minimum_balance = minimum_balance
def withdraw(self, amount):
if self.balance - amount < self.minimum_balance:
print "Sorry, you need to maintain minimum balance"
else:
return BankAccount.withdraw(self, amount)
def __repr__(self):
return "MinimuBalanceAccount, Balance: %d" %(self.balance)
if __name__ == "__main__":
a = MinimumBalanceAccount(500)
print a.deposit(5000)
print a.withdraw(4500)
print a.withdraw(500)
一些案例来自 python practice book.
源码下载Github repo: https://github.com/kracekumar/python-training