Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:05:18+00:00 2026-06-15T08:05:18+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:05:20+00:00Added an answer on June 15, 2026 at 8:05 am

    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!")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can I add custom methods/attributes to built-in Python types? In Ruby you
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Special (magic) methods in Python who can tell me what can call
Possible Duplicate: Some built-in to pad a list in python I have a method
Possible Duplicate: PhpMailer vs. Swiftmailer? I have always used PHP's built in mail() function
Possible Duplicate: Can I run from command line program created by Eclipse? I am
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: Can we use any other TAG inside <ul> along with <li>? Or
Possible Duplicate: Can I comment a JSON file? We are using a .json file
Possible Duplicate: Can not connect to Oracle via VBA - Driver's SQLSetConnectAttr Failed I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.