is
不检查班级成员资格。is
检查两个对象是否相同id
>>> isinstance(a, tuple)
True
也不type(a)
是tuple
,a
是的子类tuple
。
如果您键入verbose=True
,您可以看到它是如何制作的(文本是动态生成的以创建类):
>>> sgn_tuple = namedtuple('sgnt',['signal','type'],verbose=True)
class sgnt(tuple):
'sgnt(signal, type)'
__slots__ = ()
_fields = ('signal', 'type')
def __new__(_cls, signal, type):
'Create new instance of sgnt(signal, type)'
return _tuple.__new__(_cls, (signal, type))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new sgnt object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != 2:
raise TypeError('Expected 2 arguments, got %d' % len(result))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return 'sgnt(signal=%r, type=%r)' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
__dict__ = property(_asdict)
def _replace(_self, **kwds):
'Return a new sgnt object replacing specified fields with new values'
result = _self._make(map(kwds.pop, ('signal', 'type'), _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)
signal = _property(_itemgetter(0), doc='Alias for field number 0')
type = _property(_itemgetter(1), doc='Alias for field number 1')
这只是exec
由 Python 编辑的。我希望这能解决问题。