Mac*_*wel 5 python overriding metaprogramming namedtuple
我想创建自己的内置命名元组类型,它具有一些额外的功能。假设我们创建一个类:
from collections import namedtuple
MyClass = namedtuple('MyClass', 'field1 field2')
Run Code Online (Sandbox Code Playgroud)
它是一成不变的,易读的和简单的。现在,我可以创建MyClass的实例:
myobj = MyClass(field1 = 1, field2 = 3.0)
print(myobj.field1, myobj.field2)
Run Code Online (Sandbox Code Playgroud)
我的额外要求是创建实例时,我想检查的field1
是int
type和field2
is float
。例如,如果用户尝试创建MyClass实例:
obj = MyClass(field1 = 1, field2 = 3.0) # instantiates ok
obj1 = MyClass(field1 = 'sometext', field2 = 3.0) # raises TypeError
Run Code Online (Sandbox Code Playgroud)
我试图制作一个自定义的namedtuple,它可以验证数据类型(MyClass应该是不可变的),例如:
MyClass = modifiednamedtuple('MyClass', 'field1 field2', (int, float) )
Run Code Online (Sandbox Code Playgroud)
但是卡住了:(。namedtuple
是函数(不能是ModifyedNamedtuple的基类),我对元类的实验失败了。
有任何提示或建议吗?
好的,我想出了一个可能不是“干净”或pythonic的解决方案。除了我的对象不是一成不变的以外,它都可以工作。如何使它们不变?有什么建议可以使它更干净,更可重用吗?
这是我的代码:
def typespecificnamedtuple(name, *attr_definitions):
def init(self, *args, **kwargs):
valid_types = dict(attr_definitions) # tuples2dict
for attr_name, value in kwargs.items():
valid_type = valid_types[attr_name]
if not isinstance(value, valid_type):
raise TypeError('Cannot instantiate class '+ self.__name__+
'. Inproper datatype for '+ attr_name + '=' + str(value)+
', expected '+str(valid_type) )
setattr(self, attr_name, value)
class_dict = {'__init__' : init, '__name__' : name}
for attr_def in attr_definitions:
class_dict[attr_def[0]] = attr_def[1] # attr_def is ('name', <type int>)
customType = type(name, (object, ), class_dict )
return customType
if __name__ == '__main__':
MyClass = typespecificnamedtuple('MyClass', ('value', int), ('value2', float) )
mc = MyClass(value = 1, value2 = 3.0)
mc.something = 1 # this assigment is possible :( how to make immutable?
print(mc.__name__, mc.value, mc.value2, mc.something)
mc1 = MyClass(value = 1, value2 = 'sometext') # TypeError exception is raised
Run Code Online (Sandbox Code Playgroud)
和控制台输出:
MyClass 1 3.0 1
Traceback (most recent call last):
File "/home/pawel/workspace/prices/prices.py", line 89, in <module>
mc1 = MyClass(value = 1, value2 = 'sometext') # TypeError exception is raised
File "/home/pawel/workspace/prices/prices.py", line 70, in init
', expected '+str(valid_type) )
TypeError: Cannot instantiate class MyClass. Inproper datatype for value2=sometext, expected <class 'float'>
Run Code Online (Sandbox Code Playgroud)
namedtuple
正如你所指出的,它不是一个类;这是一个函数。但它是一个返回类的函数。因此,您可以将调用结果namedtuple
用作父类。
由于它是不可变的,所以 anamedtuple
被初始化为 in__new__
而不是 in __init__
。
所以像这样,也许:
MyTuple = namedtuple('MyTuple', 'field1 field2')
class MyClass(MyTuple):
def __new__(cls, field1, field2):
if not isinstance(field1, int):
raise TypeError("field1 must be integer")
# accept int or float for field2 and convert int to float
if not isinstance(field1, (int, float)):
raise TypeError("field2 must be float")
return MyTuple.__new__(cls, field1, float(field2))
Run Code Online (Sandbox Code Playgroud)