4 ms·
Quoting the code as it is doesn't show its development history. The cache_info could have been a tuple originally then updated to a namedtuple. Regardless, thr
by xapata 9y ago
Quoting the code as it is doesn't show its development history. The cache_info could have been a tuple originally then updated to a namedtuple.
Regardless, through conversation I am confident that at least one of the core devs thinks about namedtuple in that manner. Not as namedtuple's only purpose, but as one major benefit. Further, the core devs are not a single entity, but many people who don't always agree with each other.
Edit: note Raymond's comment elsewhere on this page.
- eesmith 9y agoYou are correct, and I apologize for not looking into this more deeply first. For the specific case under discussion, ncoghlan added cache_info() on 30 Nov 2010 - https://github.com/python/cpython/commit/234515afe594e5f9ca1b3f460735c68b04c031e2#diff-da04cc419a12dd6f8b6f44e11a459ef https://github.com/python/cpython/commit/234515afe594e5f9ca1... Previously, "Performance statistics stored in f.cache_hits and f.cache_misses." With the change "Significant statistics (maxsize, size, hits, misses) are available through the f.cache_info() named tuple." There was no time when it was a tuple, much less in a tuple in a public release. I'll look first at the namedtuples which have been present since at least 2.7.10. The named tuples in "urllib/parse", "difflib", "inspect", "sched", and "doctest" were originally tuples. The decimal module's uses a DecimalTuple as a return value from to_tuple(), so I didn't check that history. ssl uses a DefaultVerifyPaths added by tiran on 9 Jun 2013 - https://github.com/python/cpython/commit/6d7ad13a458afdf2cbdd0c3349b0589d7776cc8a#diff-c49248c7181161e24048bec5e35ba953 https://github.com/python/cpython/commit/6d7ad13a458afdf2cbd... . It did not replace a tuple. I assume _ASN1Object is the same way. Now for the namedtuples in recent version control: dis.py:_Instruction was added in https://github.com/python/cpython/commit/b39fd0c9b8dc6683924205265ff43cc597d1dfb9#diff-84f8560ccab8ce5d2705b509e1b67c29 https://github.com/python/cpython/commit/b39fd0c9b8dc6683924... . It did not replace a tuple. aifc.py:_aifc_params replaces a tuple. crypt.py:_Method replaced a class with a namedtuple in https://github.com/python/cpython/commit/daa5799cb88667855430bb49f428a4ff6c093456#diff-b913c52417d4f87c7eeb27cdfbb91ffd https://github.com/python/cpython/commit/daa5799cb8866785543... . nntplib.py:GroupInfo and ArticleInfo added in https://github.com/python/cpython/commit/69ab95105f5105b8337c757346c899d6c7e7d9bb https://github.com/python/cpython/commit/69ab95105f5105b8337... . It did not replace a tuple. pkgutil.py:ModuleInfo replaces a tuple platform.py:uname_result replaces a tuple sndhdr.py:SndHeader replaces a tuple sunau.py:_sunau_params replaces a tuple tokenize.py:TokenInfo replaces a tuple typing.py:nm_tpl ... is above my paygrade to understand. I believe it's required to be a namedtuple since that what it's trying to implement. wave.py:_wave_params replaces a tuple. Finally, urllib/robotparser.py appears to contain a bug in the following: req_rate = collections.namedtuple('req_rate', 'requests seconds') entry.req_rate = req_rate entry.req_rate.requests = int(numbers[0]) entry.req_rate.seconds = int(numbers[1]) As I read it, this should fail as the req_rate is immutable. This appears to be written by someone who thinks that it's a lightweight object. The commit is https://github.com/python/cpython/commit/960e848f0d32399824d61dce2ff5736bb596ed44#diff-cf9fffa7c76f65305ebf434feffede7a https://github.com/python/cpython/commit/960e848f0d32399824d... . It did not replace a tuple. In conclusion, 8 of the current uses of namedtuple are not to emulate a tuple. 13 of the current uses replace a tuple.
- xapata 9y agoGood research. If you found a bug, report it!
- eesmith 9y agoThank you. I will not. I reported a bug once and did not find the process at all enjoyable. It felt like several of the people in the thread thought my use case was stupid, and were quite blunt about it, to the point where I questioned my own competency. This bug isn't important enough for me to deal with that environment again.
- xapata 9y agoThat's frustrating. I've faced similar questions about use case, but the tone was simply that I'm an outlier rather than incompetent. Other times, the response has been quite pleasant and appreciative. I suppose it depends on which module you're reporting and who gets notified.
- xapata 9y agoBTW, what tool did you use for your commit/diff search?
- eesmith 9y agoBrute force. grep to find where the namedtuples are used in version control and in Python 2.7, 'git annotate' to find the last time the 'namedtuple' code was touched, then manually browse the github history view to search for when the code was added. If a given function was in python2.7 then it was easier. I checked if the code used to be a tuple.
- xapata 9y agoAh well. As far as I know, there's only BigQuery for full search of GitHub commits. They have some free usage, but it can quickly add up to $$ if you're not careful to restrict the query space.