<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Jan 11, 2014, at 11:04 PM, Yury Selivanov <<a href="mailto:yselivanov.ml@gmail.com">yselivanov.ml@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-family: ArialMT; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">I propose to add a baseclass for all namedtuples. Right now 'namedtuple'</div><div style="font-family: ArialMT; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">function dynamically creates a class derived from 'tuple', which complicates</div><div style="font-family: ArialMT; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">things like dynamic dispatch.</div></blockquote><div><br></div><div>A named tuple is a protocol, not a class. </div><div><br></div><div>Here's the glossary entry:</div><div><br></div><div>'''</div><div><dt id="term-named-tuple" style="font-weight: bold; font-size: 1.1em; font-family: sans-serif; background-color: rgb(255, 255, 255); position: static; z-index: auto;">named tuple</dt><dd style="margin-top: 3px; margin-bottom: 10px; margin-left: 30px; text-align: justify; line-height: 20px; font-family: sans-serif; background-color: rgb(255, 255, 255); position: static; z-index: auto;"><p class="first" style="margin-top: 0px !important;">Any tuple-like class whose indexable elements are also accessible using named attributes (for example, <a class="reference internal" href="library/time.html#time.localtime" title="time.localtime" style="color: rgb(53, 95, 124); text-decoration: none;"><tt class="xref py py-func docutils literal" style="background-color: transparent; padding: 0px 1px; font-size: 0.95em; font-weight: bold;"><span class="pre">time.localtime()</span></tt></a> returns a tuple-like object where the <em>year</em> is accessible either with an index such as <tt class="docutils literal" style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span class="pre">t[0]</span></tt> or with a named attribute like <tt class="docutils literal" style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span class="pre">t.tm_year</span></tt>).</p><p class="last" style="margin-top: 0px;">A named tuple can be a built-in type such as <a class="reference internal" href="library/time.html#time.struct_time" title="time.struct_time" style="color: rgb(53, 95, 124); text-decoration: none;"><tt class="xref py py-class docutils literal" style="background-color: transparent; padding: 0px 1px; font-size: 0.95em; font-weight: bold;"><span class="pre">time.struct_time</span></tt></a>, or it can be created with a regular class definition. A full featured named tuple can also be created with the factory function <a class="reference internal" href="library/collections.html#collections.namedtuple" title="collections.namedtuple" style="color: rgb(53, 95, 124); text-decoration: none;"><tt class="xref py py-func docutils literal" style="background-color: transparent; padding: 0px 1px; font-size: 0.95em; font-weight: bold;"><span class="pre">collections.namedtuple()</span></tt></a>. The latter approach automatically provides extra features such as a self-documenting representation like <tt class="docutils literal" style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span class="pre">Employee(name='jones',</span> <span class="pre">title='programmer')</span></tt>.</p></dd><div><br></div></div><div>'''</div><br><blockquote type="cite"><div style="font-family: ArialMT; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"> Basically, the only way of checking if an object</div><div style="font-family: ArialMT; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">is an instance of 'namedtuple' is to do "isinstance(o, tuple) and hasattr(o, '_fields')".</div></blockquote></div><div><br></div><div>Yes, that is the correct way of doing it.</div><div><br></div><div>ABCs weren't meant to replace all instances of duck typing.</div><div><br></div><div><br></div><div>Raymond</div><br><div><br></div><div>P.S. Here's a link to previous discussion on the subject: <a href="http://bugs.python.org/issue7796">http://bugs.python.org/issue7796</a></div><div><br></div><div><br></div></body></html>