range question, compared to Smalltalk
fb at frank-buss.de
Wed Aug 28 00:08:09 CEST 2002
Tim Peters <tim.one at comcast.net> wrote:
>> Looks like "range" returns a normal list.
>> Why not an object?
> A list is an object.
Yes, you're right :-)
What I mean: Is there any reason, why one shouldn't derive a class from
the list-class (perhaps called 'interval'), which behaves like a list, but
has only 2 attributes: start and end value. All methods and operators on
this object are overloaded, so that it looks like a list for callers, but
is much less memory consuming.
As I said, I'm a beginner in Python, but here are a start of such a class
and the range re-definition:
def __init__(self, start, end):
self.start = start
self.end = end
def __getitem__(self, index):
return self.start + index
def range(start, end):
return Interval(start, end)
i = range(123456, 123456789012)
There are much more operators and methods missing, but it should be
possible, shouldn't it? Are there any conflicts or performance hits with
existing code, if someone would implement it as standard Python?
BTW: is there a way to re-define the builtin range?
>> I thought everything is an object
> That's so, yes.
Another question: Why isn't it possible to get attributes from ints, but
File "<stdin>", line 1
SyntaxError: invalid syntax
Frank Buß, fb at frank-buss.de
More information about the Python-list