Question regarding the __kwdefaults__ output being None

Peter Otten __peter__ at web.de
Fri Apr 19 08:23:48 EDT 2019


Arup Rakshit wrote:

> I have a very basic function.
> 
> def greet(name, msg = "Good morning!"):
>    """
>    This function greets to
>    the person with the
>    provided message.
> 
>    If message is not provided,
>    it defaults to "Good
>    morning!"
>    """
> 
>    print("Hello",name + ', ' + msg)
> 
> Now when I am calling __kwdefaults__ on the function I am getting None.
> 
> 3.7.3 (default, Mar 27 2019, 09:23:15)
> [Clang 10.0.1 (clang-1001.0.46.3)]
> Python Type "help", "copyright", "credits" or "license" for more
> information. from python_methods import greet
> greet("Kate")
> Hello Kate, Good morning!
> greet("Bruce","How do you do?")
> Hello Bruce, How do you do?
> print(greet.__kwdefaults__)
> None
> 
> What am I missing here? Should not I get {“msg”: “Good morning!”} ?

There are defaults for arguments that can be either positional or passed as 
a keyword -- and there are keyword-only arguments. __kwdefaults__ is used 
for keyword-only arguments, other defaults are stored in __defaults__:

>>> def f(a, b="kw", *, c="kwonly"): pass
... 
>>> f.__kwdefaults__
{'c': 'kwonly'}
>>> f.__defaults__
('kw',)




More information about the Python-list mailing list