def read(self, str=None, filename=None, addr=0):
""" Read binary data and return a store object. The data
store is also saved in the interal 'data' attribute.
The data can either be taken from a string (str
argument) or a file (provide a filename, which will
be read in binary mode). If both are provided, the str
will be used. If neither is provided, an ArgumentError
is raised.
"""
if str is None:
if filename is None:
raise ArgumentError('Please supply a string or a filename')
file = open(filename, 'rb')
str = file.read()
file.close()
...
... # rest of code
好问题。我也解决了这个问题,虽然我同意“工厂”(类方法构造函数)是一个很好的方法,但我想建议另一种方法,我也发现它非常有用:
下面是一个示例(这是一个
read
方法,不是构造函数,但思想是一样的):这里的关键思想是使用Python对命名参数的出色支持来实现这一点。现在,如果我想从文件中读取数据,我说:
要从字符串中读取它,我说:
这样,用户只需调用一个方法。正如您所看到的,在内部处理它并不太复杂
使用python3,您可以使用Python食谱中所写的Implementing Multiple Dispatch with Function Annotations:
它的工作原理如下:
获取“备用构造函数”的一种更简洁的方法是使用类方法。例如:
它更简洁的原因是,对于预期的数据类型没有疑问,并且您不会被迫猜测调用者打算让您如何处理它提供给您的数据类型。
isinstance(x, basestring)
的问题在于,调用者无法告诉您,例如,即使类型不是基串,您也应该将其视为字符串(而不是另一个序列)。而且调用者可能希望将同一类型用于不同的目的,有时作为单个项,有时作为项序列。明确化消除了所有疑问,并导致更健壮、更清晰的代码相关问题 更多 >
编程相关推荐