Python does not recognize the type of a namedtuple -
I suspect that this question has been asked before, but I can not find it, so here goes:
On Python (using 2.7) I create a Why is that so? I hope In addition to This is the Python by bus namedtuple below:
& gt; & Gt; & Gt; Sgn_tuple = nametuple ('sgnt', ['signal', 'type']) & gt; & Gt; & Gt; I want to check the type of
t and my results are weird: <>
& gt; & Gt; & Gt; Type (A) & lt; Class' __main __. Sgnt '& gt; & Gt; & Gt; & Gt; A tube fall and gt; & Gt; & Gt; A named Tupal Fail is & gt; & Gt; & Gt; A sgnt traceback (most recent call is final): File "& lt; stdin>", line 1, & lt; Module & gt; Name error: name 'sgnt' is not defined & gt; & Gt; & Gt; A sgn_tuple false & gt; & Gt; & Gt;
a should be identified as the Tupal type, but it is not. Any signals?
does not check the membership of the class
is < / Code> checks that if two objects are the same, then
id
& gt; & Gt; & Gt; Isinstance (a, tuple) true
type (a) is not
tuple ,
a If you type
verbose = true , you can see how it is made (text dynamically class To generate):
& gt; & Gt; & Gt; Sgn_tuple = nametuple ('sgnt', ['signal', 'type'], verbose = true) class sgnt: 'sgnt (sign, type)' __slots__ = () _fields = ('signal', 'type' ) Def __new __ (_ cls, signal, type): Create a new instance of 'sgnt (sign, type)' Return _play .__ new__ (_ CLS, (signal, type)) @ classmathap def _make (CLS, RTR, New = Tupal.__nu, Len = len): 'Create a new sgnt object from the sequence or turn on' result = new (CLS, RTR) if LAN (results)! = 2: Increase type error ('expected 2 arguments,% d'% len found result)) Return result def __repr __ (self): 'Return a well formatted representation string' return 'sgnt (signal =% R, type =% R) '% self-def _asdict (self):' A new order come back which was ordered to return the values of the map fields (zip (self._fields, themselves)) __dict__ = property (_asdict) def _replace (_self, ** kwds): 'A new sgnt object returns to replace the specified areas with' new values' result = _self _ mac (Map (kwds.pop, ('signal', 'type'), _self)) if kwds: increase valueError ('unexpected field name found:% r'% kwds.keys ()) Return result def __getnewargs __ (self ): 'Return yourself as a plain tubal used by copy and pickle. 'Return to duplicate (self) signal = _property (_itemgetter (0), doc =' alias for field number 0 ') type = _property (_itemgetter (1), doc =' alias for field number1 ')
exec Aid I hope that clears things up.
Comments
Post a Comment