5

Possible Duplicate:
How can the built-in range function take a single argument or three?

The Python documentation for slice() lists the following method signatures:

slice(stop)
slice(start, stop[, step])

Here are a few examples of creating a slice object with different numbers of arguments:

>>> slice(8)
slice(None, 8, None)
>>> slice(4, 8)
slice(4, 8, None)
>>> slice(4, 8, 2)
slice(4, 8, 2)

Note that if you call slice() with a single argument, that argument is used as the stop attribute (second argument with the two or three argument signature).

Since function overloading does not exist in Python, the typical way to allow a variable number of arguments is to use None as a default value, for example one attempt to reproduce the above behavior would be the following:

class myslice(object):
    def __init__(self, start, stop=None, step=None):
        if stop is None and step is None:
            # only one argument was provided, so use first argument as self.stop
            self.start = None
            self.stop = start
        else:
            self.start = start
            self.stop = stop
        self.step = step

    def __repr__(self):
        return 'myslice({}, {}, {})'.format(self.start, self.stop, self.step)

However this assumes that None is not a value that could be provided for stop, note the following difference in behavior between my implementation and slice():

>>> slice(1, None)
slice(1, None, None)
>>> myslice(1, None)
myslice(None, 1, None)

Is there some other value that I could use in place of None that would indicate that the parameter was definitely not provided? Alternatively, is there a different way to implement this behavior other than default argument values?

Edit: By the way, in CPython, slice() is implemented in C.

martineau
  • 119,623
  • 25
  • 170
  • 301
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • Not a duplicate, since `None` is not a valid argument to `range()` so my implementation would work okay with `range()` but not with `slice()`. – Andrew Clark Nov 29 '12 at 19:43
  • 1
    +1 for your answer to that question, but I still don't think that this question is a duplicate even if the answer happens to apply to both. The OP of that question could decide to accept a different answer that suggests using `None` for default values, which wouldn't apply here. – Andrew Clark Nov 29 '12 at 23:27
  • Sure, but they didn't. It's the same conceptual underpinning, the same basic approaches to answering the question, and ultimately almost identical code. – Marcin Dec 03 '12 at 14:14

1 Answers1

11

There are a few ways to deal with the default argument issue. None is the most common, but when None is actually a reasonable value to pass in, you can create a sentinel:

sentinel = object()
def func(arg=sentinel):
    if arg is sentinel:
       arg = "default"
    do_code()

Here's a related answer where there was some discussion about using Ellipsis as the sentinel as ... is also a singleton.


An alternative way to do this would be to use *args and unpack:

def func(*args):
   if len(args) == 3:
       start,stop,step=args
   elif len(args) == 2:
       start,stop = args
       step = None
   elif len(args) == 1:
       stop, = args  #Need the comma here.  Could write `stop = args[0]` instead ...
       step = start = None
   else:
       raise TypeError("Wrong number of arguments!")
Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696