[Python-ideas] List Revolution
Michael Foord
fuzzyman at gmail.com
Mon Sep 12 01:41:45 CEST 2011
On 9 September 2011 22:12, Christopher King <g.nius.ck at gmail.com> wrote:
> The first element in a list is element zero, the second is one, the third
> it two, and so on. This some times confuses newbies to the language or
> programming in general. This system was invited when single bits where
> precious. It's time to update. Keep in mind this is something for version 4,
> since its not reverse compatible. I say we make the first element 1, second
> 2, third 3, and so on. Other languages would follow. We are python, made for
> easiness and readability, and we are in the age where you can't even get
> something as small as a kilobyte USB. We must make first one, second 2, and
> third 3, like it is supposed to be.
How about this (works with current and future pythons):
>>> class Index(int):
... def __new__(cls, val):
... return int.__new__(cls, val -1)
... def __repr__(self):
... return 'Index(%s)' % (int(self) + 1)
...
>>> thing = list(range(1, 11))
>>>
>>> thing[Index(1)]
1
>>> thing
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> thing[Index(2)]
2
>>> thing[Index(3)]
3
>>> Index(1)
Index(1)
Michael Foord
> I give this:
> *+1*
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110912/0bdb4142/attachment.html>
More information about the Python-ideas
mailing list