习题41、物以类聚
#!/usr/bin/python
# -*- coding:utf-8 -*-
class TheThing(object):
def __init__(self):
self.number = 0
def some_function(self):
print "I got called."
def add_me_up(self, more):
self.number += more
return self.number
# two different things
a = TheThing()
b = TheThing()
a.some_function()
b.some_function()
print a.add_me_up(20)
print b.add_me_up(30)
print a.number
print b.number
class TheMultiplier(object):
def __init__(self, base):
self.base = base
def do_it(self, m):
return m * self.base
x = TheMultiplier(a.number)
print x.do_it(b.number)
你看到参数里的
self
了吧?你知道它是什么东西吗?对了,它就是 Python 创建的额外的一个参数,有了它你才能实现a.some_function()
这种用法,这时它就会把\ 前者翻译成some_function(a)
执行下去。为什么用self
呢?因为你的函数并不知道你的这个“实例”是来自叫 TheThing 或者别的名字的class
。所以你只要使用一个通用的名字self
。这样你写出来的函数就会在任何情况下都能正常工作。
习题42、对象、类、以及从属关系
#!/usr/bin/python
# -*- coding:utf-8 -*-
#重写41,不采用class
class Animal(object):
pass
class Dog(Animal):
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init__(self, name):
self.name = name
class Person(object):
def __init__(self, name):
self.name = name
self.pet = None
class Employee(Person):
def __init__(self, name, salary):
super(Employee, self).__init__(name)
self.salary = salary
class Fish(object):
pass
class Salmon(Fish):
pass
class Halibut(Fish):
pass
rover = Dog("Rover")
satan = Cat("Satan")
mary = Person("Mary")
小结
- 这句
self.pet = None
有什么用?
确保类的self.pet
属性被设置为None
。super(Employee, self).__init__(name)
是做什么用的?
这样你可以可靠地将父类的__init__
方法运行起来。搜索“python super”,看看它的优缺点。
习题43、
cities['_find'] = find_city
city_found = cities['_find'](cities, state)
第二行代码可以分解成如下步骤:
- Python 看到
city_found =
于是知道了需要创建一个变量。- 然后它读到
cities
,然后知道了它是一个字典- 然后看到了
['_find']
,于是 Python 就从索引找到了字典cities
中对应的位置,并且获取了该位置的内容。['_find']
这个位置的内容是我们的函数find_city
,所以 Python 就知道了这里表示一个函数,于是当它碰到(
就开始了函数调用。cities, state
这两个参数将被传递到函数find_city
中,然后这个函数就被运行了。find_city
接着从cities
中寻找states
,并且返回它找到的内容,如果什么都没找到,就返回一个信息说它什么都没找到。- Python
find_city
接受返回的信息,最后将该信息赋值给一开始的city_found
这个变量。
import random
from urllib import urlopen
import sys
WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []
PHRASES = {
"class ###(###):":
"Make a class named ### that is-a ###.",
"class ###(object):\n\tdef __init__(self, ***)" :
"class ### has-a __init__ that takes self and *** parameters.",
"class ###(object):\n\tdef ***(self, @@@)":
"class ### has-a function named *** that takes self and @@@ parameters.",
"*** = ###()":
"Set *** to an instance of class ###.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
}
# do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
# load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("###"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = []
for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS, param_count)))
for sentence in snippet, phrase:
result = sentence[:]
# fake class names
for word in class_names:
result = result.replace("###", word, 1)
# fake other names
for word in other_names:
result = result.replace("***", word, 1)
# fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
# keep going until they hit CTRL-D
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"
习题44、继承(Inheritance) VS 合成(Composition)
父类和子类有三种交互方式:
- 子类上的动作完全等同于父类上的动作
- 子类上的动作完全改写了父类上的动作
- 子类上的动作部分变更了父类上的动作
隐式继承
#!/usr/bin/python
# -*- coding:utf-8 -*-
#隐式继承 Implicit Inheritance
class Parent(object):
def implicit(self):
print "PARENT implicit()"
class Child(Parent):
pass
dad = Parent()
son = Child()
dad.implicit()
son.implicit()
显示继承
#!/usr/bin/python
# -*- coding:utf-8 -*-
#显式覆写
class Parent(object):
def override(self):
print "PARENT override()"
class Child(Parent):
def override(self):
print "CHILD override()"
dad = Parent()
son = Child()
dad.override()
son.override()
在运行前或运行后覆写
#!/usr/bin/python
# -*- coding:utf-8 -*-
综合
#!/usr/bin/python
# -*- coding:utf-8 -*-
class Parent(object):
def override(self):
print "PARENT override()"
def implicit(self):
print "PARENT implicit()"
def altered(self):
print "PARENT altered()"
class Child(Parent):
def override(self):
print "CHILD override()"
def altered(self):
print "CHILD, BEFOR PARENT altered()"
super(Child, self).altered()
print "CHILD, AFTER PARENT altered()"
dad = Parent()
son = Child()
dad.implicit()
son.implicit()
dad.override()
son.override()
dad.altered()
son.altered()
习题45、你来制作一个游戏
函数的风格
- 1.
类的风格
- 1.
代码风格
- 1.
好的注释