def namedtuple(typename, field_names, verbose=False, rename=False):
"""Returns a new subclass of tuple with named fields.
namedtuple是一个很神奇的东西,在看kafka-python的时候看到了很多地方用到了这玩意。用法也非常简单
from collections import namedtuple
People = namedtuple(typename="people",field_names=["sex","name"],verbose=True)
print People(sex="f",name="abc")
很方便的创建一个父类为tuple的类,且可以用kv对的方式创建对象。
怎么实现的嘞?我能最快想到的方法大概是使用type直接创建一个类。
def __new__(_cls, sex, name):
return tuple.__new__(_cls, (sex, name))
People2 = type("People2",(tuple,),dict(__new__=__new__))
print People2(sex="f",name="abc")
大概是这样的,但是还有一些问题,比如打印出来的样子啦 之类的,虽然麻烦点,总归是能做的。
又或者是使用metaclass搞个类工厂,会比type方便不少。
带着疑问,点开了namedtuple的源码。
class_definition = _class_template.format(
typename = typename,
field_names = tuple(field_names),
num_fields = len(field_names),
arg_list = repr(tuple(field_names)).replace("'", "")[1:-1],
repr_fmt = ', '.join(_repr_template.format(name=name)
for name in field_names),
field_defs = '\n'.join(_field_template.format(index=index, name=name)
for index, name in enumerate(field_names))
)
# Execute the template string in a temporary namespace and support
# tracing utilities by setting a value for frame.f_globals['__name__']
namespace = dict(_itemgetter=_itemgetter, __name__='namedtuple_%s' % typename,
OrderedDict=OrderedDict, _property=property, _tuple=tuple)
try:
exec class_definition in namespace
except SyntaxError as e:
raise SyntaxError(e.message + ':\n' + class_definition)
result = namespace[typename]
感觉画风有点不太对了..._class_template是个啥玩意,咋还用exec嘞?
_class_template = '''\
class {typename}(tuple):
'{typename}({arg_list})'
__slots__ = ()
_fields = {field_names!r}
def __new__(_cls, {arg_list}):
'Create new instance of {typename}({arg_list})'
return _tuple.__new__(_cls, ({arg_list}))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new {typename} object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != {num_fields:d}:
raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return '{typename}({repr_fmt})' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
def _replace(_self, **kwds):
'Return a new {typename} object replacing specified fields with new values'
result = _self._make(map(kwds.pop, {field_names!r}, _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % kwds.keys())
return result
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
__dict__ = _property(_asdict)
def __getstate__(self):
'Exclude the OrderedDict from pickling'
pass
{field_defs}
'''
哦。
网友评论