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")
很方便的創(chuàng)建一個父類為tuple的類,且可以用kv對的方式創(chuàng)建對象。
怎么實現(xiàn)的嘞?我能最快想到的方法大概是使用type直接創(chuàng)建一個類步藕。
def __new__(_cls, sex, name):
return tuple.__new__(_cls, (sex, name))
People2 = type("People2",(tuple,),dict(__new__=__new__))
print People2(sex="f",name="abc")
大概是這樣的,但是還有一些問題挑格,比如打印出來的樣子啦 之類的咙冗,雖然麻煩點(diǎn),總歸是能做的漂彤。
又或者是使用metaclass搞個類工廠雾消,會比type方便不少灾搏。
帶著疑問,點(diǎn)開了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]
感覺畫風(fēng)有點(diǎn)不太對了..._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}
'''
哦桑腮。