我的程序需要做两件事情。
从网页中提取信息。
对网页进行操作。
然而,有许多网页,比如 Twitter 和 Facebook。
我该怎么办?
def facebookExtract():
code here
def twitterExtract():
code here
def myspaceExtract():
code here
def facebookProcess():
code here
def twitterProcess():
code here
def myspaceProcess():
code here
或者,我是否应该有某种类?何时建议使用类,何时建议仅使用函数?
if
语句(例如)不是一个对象;这与 Smalltalk 非常不同,在 Smalltalk 中没有 "if 语句",而是 Booleans 的一个方法,因此是一个对象。Smalltalk 是狂热的面向对象编程(你必须疯了才会用其他范式编写 Smalltalk!),Python 只是实用主义的面向对象编程(在 Python 中有很多情况下,非面向对象的方法,特别是FP方法,显然更可取)。关于抽象,参见 http://us.pycon.org/2009/conference/schedule/event/75/ -- 我认为我在那里讨论了这个问题。;-) - Alex Martelli