# 第7章 使用函数和模块来重用你的代码

#### 7.1 使用函数

###### 7.1.1 函数的组成部分

``````>>> def testfunc(myname):
print('hello %s' % myname)

>>> testfunc('Tao1024')

hello Tao1024
>>> testfunc('麦典威')

hello 麦典威
``````

``````>>> def savings(pocket_money, paper_route, spending):
return pocket_money + paper_route-spending

>>> print(savings(10,10,5))

15
``````
###### 7.1.2 变量和作用域

``````>>> def variable_test():
first_variable = 10
second_variable = 20
return first_variable*second_variable

>>> print(variable_test())
200
>>> print(first_variable)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print(first_variable)
NameError: name 'first_variable' is not defined
``````

``````>>> another_variable = 100
>>> def variable_test():
first_variable = 10
second_variable = 20
return first_variable*second_variable*another_variable

>>> print(variable_test())
20000
>>> print(another_variable)
100
``````

``````>>> def spaceship_building(cans):
total_cans = 0
for week in range(1,53):
total_cans = total_cans + cans
print('Week %s = %s cans' % (week, total_cans))

>>> spaceship_buidling(2)
>>> spaceship_building(13)
``````

#### 7.2 使用模块

``````>>> import time
>>> print(time.asctime())
Sat Mar 31 14:36:17 2018
``````
``````>>> import sys
hello
hello
``````
``````>>> def silly_age_joke():
print('How old are you?')
if age >= 10 and age <= 13:
print('What is 13 + 49 + 84 +155 + 97？ A headache! ')
else:
print('Huh?')

>>> silly_age_joke()
How old are you?
12
What is 13 + 49 + 84 +155 + 97？ A headache!
``````

# 第8章 如何使用类和对象

#### 8.1 把事物拆分成类

``````>>> class Things:
pass
``````
###### 8.1.1 父母与孩子

``````>>> class Things:
pass

>>> class Animate(Things):
pass

>>> class Inanimate(Things):
pass
``````
###### 8.1.2 增加属于类的对象

``````>>> class Giraffes():
pass

>>> reginald = Giraffes()
``````
###### 8.1.3 定义类中的函数
``````>>> class ThisIsMySillyClass:
def this_is_a_class_function():
print('I am a class function')
def this_is_also_a_class_function():
print('I am also a class function. See?')
``````
###### 8.1.4 用函数来表示类的特征

``````>>> class Animals():
def breathe(self):
pass
def move(self):
pass
def eat_food(self):
pass
``````

self这个参数是用来从类中的一个函数调用类中（包括父类）的另一个函数的。

###### 8.1.5 为什么要使用类和对象

``````>>> class Animals():
def breathe(self):
print('breathing')
def move(self):
print('moving')
def eat_food(self):
print('eating food')

>>> class Mammals(Animals):
def feed_young_with_milk(self):
print('feeding young')

>>> class Giraffes(Mammals):
def eat_leaves_from_trees(self):
print('eating leaves')

>>> reginald = Giraffes()
>>> harold = Giraffes()
>>> reginald.move()
moving
>>> harold.eat_leaves_from_trees()
eating leaves
``````
###### 8.1.6 画图中的对象与类
``````>>> import turtle
>>> avery = turtle.Pen()
>>> kate = turtle.Pen()
>>> avery.forward(50)
>>> avery.right(90)
>>> avery.forward(20)
>>> kate.left(90)
>>> kate.forward(100)
``````
image

#### 8.2 对象和类的另一些实用功能

###### 8.2.2 从函数里调用其他函数
``````>>> class Animals():
def breathe(self):
print('breathing')
def move(self):
print('moving')
def eat_food(self):
print('eating food')

>>> class Mammals(Animals):
def feed_young_with_milk(self):
print('feeding young')

>>> class Giraffes(Mammals):
def find_food(self):
self.move()
print('I have found food')
self.eat_food()

>>> reginald = Giraffes()
>>> reginald.find_food()
moving
I have found food
eating food
``````

#### 8.3 初始化对象

``````>>> gertrude = Giraffes(100)
>>> ozwald = Giraffes(200)
>>> print(gertrude.giraffe_sports)
100
>>> print(ozwald.giraffe_sports)
200
``````