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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:42:07+00:00 2026-06-05T10:42:07+00:00

Consider these different behaviour:: >> def minus(a, b): >> return a – b >>

  • 0

Consider these different behaviour::

>> def minus(a, b):
>>    return a - b

>> minus(**dict(b=2, a=1))
-1

>> int(**dict(base=2, x='100'))
4

>> import operator
>> operator.sub.__doc__
'sub(a, b) -- Same as a - b.'
>> operator.sub(**dict(b=2, a=1))
TypeError: sub() takes no keyword arguments

Why does operator.sub behave differently from int(x, [base]) ?

  • 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-05T10:42:09+00:00Added an answer on June 5, 2026 at 10:42 am

    It is an implementation detail. The Python C API to retrieve arguments separates between positional and keyword arguments. Positional arguments do not even have a name internally.

    The code used to retrieve the arguments of the operator.add functions (and similar ones like sub) is this:

    PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)
    

    As you can see, it does not contain any argument name. The whole code related to operator.add is:

    #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
      PyObject *a1, *a2; \
      if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
      return AOP(a1,a2); }
    
    spam2(op_add           , PyNumber_Add)
    
    #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
                               {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
    spam2(add,__add__, "add(a, b) -- Same as a + b.")
    

    As you can see, the only place where a and b are used is in the docstring. The method definition also does not use the METH_KEYWORDS flag which would be necessary for the method to accept keyword arguments.

    Generally spoken, you can safely assume that a python-based function where you know an argument name will always accept keyword arguments (of course someone could do nasty stuff with *args unpacking but creating a function doc where the arguments look normal) while C functions may or may not accept keyword arguments. Chances are good that functions with more than a few arguments or optional arguments accept keyword arguments for the later/optional ones. But you pretty much have to test it.

    You can find a discussion about supporting keyword arguments everywhere on the python-ideas mailinglist. There is also a statement from Guido van Rossum (the Benevolent Dictator For Life aka the creator of Python) on it:

    Hm. I think for many (most?) 1-arg and selected 2-arg functions (and
    rarely 3+-arg functions) this would reduce readability, as the example
    of ord(char=x) showed.

    I would actually like to see a syntactic feature to state that an
    argument cannot be given as a keyword argument (just as we already
    added syntax to state that it must be a keyword).

    One area where I think adding keyword args is outright wrong: Methods
    of built-in types or ABCs and that are overridable. E.g. consider the
    pop() method on dict. Since the argument name is currently
    undocumented, if someone subclasses dict and overrides this method, or
    if they create another mutable mapping class that tries to emulate
    dict using duck typing, it doesn’t matter what the argument name is —
    all the callers (expecting a dict, a dict subclass, or a dict-like
    duck) will be using positional arguments in the call. But if we were
    to document the argument names for pop(), and users started to use
    these, then most dict sublcasses and ducks would suddenly be broken
    (except if by luck they happened to pick the same name).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which of these two different models would be more efficient (consider thrashing, utilization of
Consider these two classes mapped to the same table. One is readonly via mutable=false.
Consider these 2 pieces of code (you can assume execeptionObj is of type Object
Consider these three mysql statements: select * from Users; select id, title, value from
Consider these pseudo models: class BaseProduct: quantity_available = Integer class Box(BaseProduct): items_in_box = Integer
Consider these several tables: tab1 ------- userid email address environment tab2 ------- ecode company
To give an idea of my requirement, consider these classes - class A {
I ran across a compilation issue today that baffled me. Consider these two container
Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target
Consider these two functions: 'Overload 1 <Extension()> Function ComputeReturnValue(Of TSource, TResult)(ByVal source As TSource,

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.