Question regarding the __kwdefaults__ output being None

Arup Rakshit ar at zeit.io
Fri Apr 19 09:06:05 EDT 2019


Hi Peter,


Thanks for explaining it. Beautiful.


Thanks,

Arup Rakshit
ar at zeit.io



> On 19-Apr-2019, at 5:53 PM, Peter Otten <__peter__ at web.de> wrote:
> 
> 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',)
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list