Issue 21444: __len__ can't return big numbers
Created on 2014-05-05 23:11 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 17934 | merged | Zac Hatfield-Dodds, 2020-01-10 12:13 | |
| Messages (7) | |||
|---|---|---|---|
| msg217957 - (view) | Author: Ram Rachum (cool-RR) * | Date: 2014-05-05 23:11 | |
I want to use big numbers for length.
>>> class A:
... __len__ = lambda self: 10 ** 20
>>> len(A())
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
len(A())
OverflowError: cannot fit 'int' into an index-sized integer
|
|||
| msg217958 - (view) | Author: R. David Murray (r.david.murray) * ![]() |
Date: 2014-05-06 00:47 | |
While this is classed as a CPython implementation detail (see issue 15718) it doesn't sound like it is likely to be changed (see issue 2723). |
|||
| msg217974 - (view) | Author: Mark Dickinson (mark.dickinson) * ![]() |
Date: 2014-05-06 09:58 | |
Whoops; sorry -- accidental title change by typing `__len__` into something that wasn't the search box. Stupid fingers... (I suspect this issue is a duplicate of an existing issue.) |
|||
| msg217983 - (view) | Author: R. David Murray (r.david.murray) * ![]() |
Date: 2014-05-06 13:46 | |
Mark: I thought it was too, but the two I noted were the closest I could find. Maybe you'll find something even more on point :) |
|||
| msg217988 - (view) | Author: Akira Li (akira) * | Date: 2014-05-06 14:59 | |
If `len()` signature can't be changed to return Python int objects (unlimited) then the OverflowError may contain the actual `.length` property instead (based on msg66459 by Antoine Pitrou) operator.length(): def length(sized): """Return the true (possibly large) length of `sized` object. It is equivalent to len(sized) if len doesn't raise OverflowError i.e., if the length is less than sys.maxsize on CPython; otherwise return OverflowError.length attribute """ try: return len(sized) except OverflowError as e: return e.length |
|||
| msg218002 - (view) | Author: Mark Dickinson (mark.dickinson) * ![]() |
Date: 2014-05-06 18:44 | |
That's pretty evil. :-) |
|||
| msg218124 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2014-05-08 21:00 | |
I recommend this be closed: too much impact on existing code for too little benefit. CPython has historically imposed some artificial implementation specific details in order make the implementation cleaner and faster internally (i.e. a limit on the number of function arguments, sys.maxsize limits, etc.) |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:03 | admin | set | github: 65643 |
| 2020-01-10 12:13:55 | Zac Hatfield-Dodds | set | pull_requests: + pull_request17341 |
| 2014-05-08 21:17:54 | benjamin.peterson | set | status: open -> closed resolution: wont fix |
| 2014-05-08 21:00:46 | rhettinger | set | priority: normal -> low versions: + Python 3.5, - Python 3.4 nosy: + rhettinger messages: + msg218124 type: enhancement |
| 2014-05-06 18:44:53 | mark.dickinson | set | messages: + msg218002 |
| 2014-05-06 14:59:41 | akira | set | nosy:
+ akira messages: + msg217988 |
| 2014-05-06 13:46:34 | r.david.murray | set | messages: + msg217983 |
| 2014-05-06 09:58:58 | mark.dickinson | set | nosy:
+ mark.dickinson messages: + msg217974 |
| 2014-05-06 09:57:56 | mark.dickinson | set | title: __len__ -> __len__ can't return big numbers |
| 2014-05-06 09:57:40 | mark.dickinson | set | title: __len__ can't return big numbers -> __len__ |
| 2014-05-06 09:53:11 | jcea | set | nosy:
+ jcea |
| 2014-05-06 00:47:34 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg217958 |
| 2014-05-05 23:11:11 | cool-RR | create | |
