问题描述:

According to the Python Tutorial (9.7. Odds and Ends):

A piece of Python code that expects a particular abstract data type

can often be passed a class that emulates the methods of that data

type instead. For instance, if you have a function that formats some

data from a file object, you can define a class with methods read()

and readline() that get the data from a string buffer instead, and

pass it as an argument.

Does this means I should pass a class object as an argument? I know that a class is essentially "a wrapper around the contents of the namespace created by the class definition", but if I already have a function to do this job (format some data from a file object), why do I need to define a class capable of doing it?

Or, in other words, to format some data from a file object, why is defining a class superior to defining a function?

Have I misunderstood this paragraph? And I would appreciate it very much if someone can provide some examples on it.

相关阅读:
Top