range question, compared to Smalltalk

Frank Buss fb at frank-buss.de
Tue Aug 27 18:08:09 EDT 2002


Tim Peters <tim.one at comcast.net> wrote:

>> Looks like "range" returns a normal list.
> 
> Right.
> 
>> 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:

class Interval:
  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)
print i[12345678901]
result: 12345802357

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 
from strings?

>>> 3.__class__
  File "<stdin>", line 1
    3.__class__
              ^
SyntaxError: invalid syntax
>>> "abc".__class__
<type 'str'>

-- 
Frank Buß, fb at frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de



More information about the Python-list mailing list