原文链接:http://learnpythonthehardway.org/book/ex38.html
你已经学习过关于列表的知识了。当你学习完关于while循环后你知道怎么在一个列表的末尾“追加”数字并打印出来。在研究训练中也让你使用Python文档去查找所有关列表的操作。现在回顾一下,如果你压根不知道我在说什么,那么就去书中找到关于列表的操作然后复习一下它。
找到了吗?还记得吗?好的。当你做这个操作之前你得先有一个列表变量,然后你才可以用这个列表变量来调用append函数。然而,你可能不是真正理解发生了什么,所有然我们看看列表到底能做什么操作。
当你在Python中输入 mystuff.append('hello')后实际上它在Python中执行了一系列的事情才引起了 mystuff 列表的改变。下面就是解释它是怎么工作的:
1、Python 看见你提到的mystuff然后就会去查找这个变量。它可能是否带有 = 的地方创建了这个变量 ,也可能是某一个函数的参数,或者可能是一个全局变量。不管什么方式它总得先找到 mystuff 的定义。
2、一旦它找到了 mystuff ,就轮到处理句点 . (period) 这个操作符,而且开始查看 mystuff 内部的一些变量了。由于 mystuff 是一个列表,Python 知道mystuff 支持一些函数。
3、接下来轮到了处理 append 。Python 会将 “append” 和 mystuff 支持的所有函数的名称一一对比,如果确实其中有一个叫 append 的函数,那么 Python 就会去使用这个函数。
4、接下来 Python 看到了括号 ( (parenthesis) 并且意识到, “噢,原来这应该是一个函数”,到了这里,它就正常会调用这个函数了,不过这里的函数还要多一个参数才行。
5、这个额外的参数其实是…… mystuff! 我知道,很奇怪是不是?不过这就是 Python 的工作原理,所以还是记住这一点,就当它是正常的好了。真正发生的事情其实是 append(mystuff, 'hello') ,不过你看到的只是 mystuff.append('hello') 。
这些大部分你不必知道的,但是当你遇到下面这样的错误的时候,这些知识将会帮助你找到问题:
c:\>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Thing(object):
... def test(hi):
... print "hi"
...
>>> a = Thing()
>>> a.test("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes exactly 1 argument (2 given)
这些都是些什么啊?好吧,这里我是直接在Pyhon脚本中输入代码演示一些神奇的东西。你可能还没见过 class ,不过没关系后面会学到它的。现在你就看看 test() takes exactly 1 argument (2 given) 这句话是怎么回事。它其实意味着 python 把 a.test("hello") 改成了 test(a, "hello") ,而有人弄错了,没有为它添加 a 这个参数。
一下消化这么多可能比较困难,所以我们先做一些练习来巩固这些概念。为了解决这个问题,这里有个混合了字符串和列表的练习,看看你能不能从里面找出点乐子来。
ten_things = "Apples Oranges Crows Telephone Light Sugar"
print "Wait there's not 10 things in that list ,let's fix that."
stuff = ten_things.split(' ')
more_stuff = ["Day" ,"Night" ,"Song" ,"Frisbee" ,"Corn" ,"Banana" ,"Girl" ,"Boy"]
while len(stuff) != 10:
next_one = more_stuff.pop();
print "Adding" ,next_one
stuff.append(next_one)
print "There's %d items now." % len(stuff)
print "There we go: ",stuff
print "Let's do some things with sutff."
print stuff[1]
print stuff[-1] #whoa! fancy
print stuff.pop()
print ' '.join(stuff) #what? cool
print '#'.join(stuff[3:5]) #super stellar!
输出结果如下:
c:\>python ex38.py
Wait there's not 10 things in that list ,let's fix that.
Adding Boy
There's 7 items now.
Adding Girl
There's 8 items now.
Adding Banana
There's 9 items now.
Adding Corn
There's 10 items now.
There we go: ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy
', 'Girl', 'Banana', 'Corn']
Let's do some things with sutff.
Oranges
Corn
Corn
Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
Telephone#Light
研究训练:
2、将这两种方式翻译为自然语言。例如, ' '.join(things) 可以翻译成“用 ‘ ‘ 连接(join) things”,而 join(' ', things) 的意思是“为 ‘ ‘ 和 things 调用 join 函数”。这其实是同一件事情。
3、上网阅读一些关于“面向对象编程(Object Oriented Programming)”的资料。晕了吧?嗯,我以前也是。别担心。你将从这本书学到足够用的关于面向对象编程的基础知识,而以后你还可以慢慢学到更多。
4、查一下 Python中的 “class” 是什么东西。不要阅读关于其他语言的 “class” 的用法,这会让你更糊涂。
5、dir(something) 和 something 的 class 有什么关系?
6、如果你不知道我讲的是些什么东西,别担心。程序员为了显得自己聪明,于是就发明了 Opject Oriented Programming,简称为 OOP,然后他们就开始滥用这个东西了。如果你觉得这东西太难,你可以开始学一下 “函数编程(functional programming)”。