我建议实现特定的数据类型,以便能够区分具有相同结构的不同类型的信息。例如,您可以简单地进行子类list
化,然后在运行时在您的函数中进行一些类型检查:
class WaveParameter(list):
pass
class Point(list):
pass
# you can use them just like lists
point = Point([1, 2, 3, 4])
wp = WaveParameter([5, 6])
# of course all methods from list are inherited
wp.append(7)
wp.append(8)
# let's check them
print(point)
print(wp)
# type checking examples
print isinstance(point, Point)
print isinstance(wp, Point)
print isinstance(point, WaveParameter)
print isinstance(wp, WaveParameter)
因此,您可以在函数中包含这种类型检查,以确保将正确类型的数据传递给它:
def example_function_with_waveparameter(data):
if not isinstance(data, WaveParameter):
log.error("received wrong parameter type (%s instead WaveParameter)" %
type(data))
# and then do the stuff
或者简单地说assert
:
def example_function_with_waveparameter(data):
assert(isinstance(data, WaveParameter))