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 6966267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:10:11+00:00 2026-05-27T16:10:11+00:00

I am reading the source codes of mercurial, and found such a func def

  • 0

I am reading the source codes of mercurial, and found such a func def in commands.py:

def import_(ui, repo, patch1=None, *patches, **opts):
    ...

in python, postional args must be put ahead of keyword args. But here, patch1 is a keyword argument, followed by a positional argument *patches. why is this OK?

  • 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-05-27T16:10:12+00:00Added an answer on May 27, 2026 at 4:10 pm

    Just have a look into the PEP 3102 also it seems its somehow related to this.

    To summarize, patches and opts are there to accept variable arguments but the later is to accept keyword arguments. The keyword arguments are passed as a dictionary where as the variable positional arguments would be wrapped as tuples .

    From your example

    def import_(ui, repo, patch1=None, *patches, **opts):
    

    Any positional parameters after u1,repo and patch1 would be wrapped as tuples in patches. Any keyword arguments following the variable positional arguments would be wrapped as Dictionary objects through opts.

    Another important thing is that, the onus lies with the caller to ensure that the condition non-keyword arg after keyword arg is not violated.

    So something that violates this would raise a syntax error..

    For example

    Calls like

     import_(1,2,3,test="test")
     import_(1,2,3,4,test="test")
     import_(1,2,3,4,5)
     import_(1,2,patch1=3,test="test")
    

    are valid, but

    import_(1,2,3,patch1=4,5)
    

    would raise a syntax error SyntaxError: non-keyword arg after keyword arg

    In the first valid case import_(1,2,3,test="test")

    u1 = 1, repo = 2, patch1 = 3, patches = () and opts={"test":"test"}
    

    In the second valid case import_(1,2,3,patch1=4,test="test")

    u1 = 1, repo = 2, patch1 = 3 , patches = (4) and opts={"test":"test"}
    

    In the third valid case import_(1,2,3,4,5)

    u1 = 1, repo = 2, patch1 = 3 , patches=(4,5), and opts={}
    

    In the fourth valid case import_(1,2,patch1=3,test="test")

    u1 = 1, repo = 2, patch1 = 3 , patches=(), and opts={"test":"test"}
    you can use patch1 as a keywords argument but doing so you cannot wrap any variable positional arguments within patches
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading knockout.js library source codes and i saw such as function calls
These days I am reading some javascript source codes,however I found some syntax that
Reading source code of sample projects, such as Beast and Bort, are recommended as
When I reading source code of Beast, I found a lot of code like
I'm reading some source code at https://github.com/plataformatec/devise and found that line of code: class_eval
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations
I am a beginner in C. While reading git's source code, I found this
I am reading some source code and I found this statement at the very
Reading some source code, I have found next traits definition: namespace dds { template
I'm reading source code of FreeBSD and found that they have 2 files which

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.