Because None
is the default for slice positions. You can use either None
or omit the value altogether, at which point None
is passed in for you.
None
is the default because you can use a negative stride, at which point what the default start and end positions are changed. Compare list[0:len(list):-1]
to list[None:None:-1]
, for example.
Python uses None
for 'value not specified' throughout the standard library; this is no exception.
Note that if your class implements the object.__getitem__
hook, you'll get passed a slice()
object with the start, end and stride attributes set to None
as well:
>>> class Foo(object):
... def __getitem__(self, key):
... print key
...
>>> Foo()[:]
slice(None, None, None)
Since Foo()
doesn't even implement a __len__
having the defaults use None
is entirely logical here.