[Python-ideas] Keyword only argument on function call

Steve Barnes gadgetsteve at live.co.uk
Tue Sep 11 00:47:37 EDT 2018



On 10/09/2018 22:00, Ethan Furman wrote:
> On 09/10/2018 12:52 PM, Chris Barker via Python-ideas wrote:
> 
>> I've spent this whole thread thinking: "who in the world is writing 
>> code with a lot of spam=spam arguments? If you are transferring that 
>> much state in a function call, maybe you should have a class that 
>> holds that state? Or pass in a **kwargs dict?
> 
>> So still looking for a compelling use-case
> 
> In my day job I spend a lot of time writing/customizing modules for a 
> framework called OpenERP (now Odoo*).  Those modules are all subclasses, 
> and most work will require updating at least a couple parent metheds -- 
> so most calls look something like:
> 
>    def a_method(self, cr, uid, ids, values, context=None):
>      ...
>      super(self, parent).a_method(cr, uid, ids, values, context=context)
> 
> Not a perfect example as these can all be positional, but it's the type 
> of code where this syntax would shine.
> 
> I think, however, that we shouldn't worry about a lead * to activate it, 
> just use a leading '=' and let it show up anywhere and it follows the 
> same semantics/restrictions as current positional vs keyword args:
> 
>    def example(filename, mode, spin, color, charge, orientation):
>        pass
> 
>    example('a name', 'ro', =spin, =color, charge=last, =orientation)
> 
> So +0 with the above proposal.
> 
> -- 
> ~Ethan~
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

Couldn't just about all of the use cases mentioned so far be met in 
quite a neat manner by providing access to a method, or dictionary, 
called __params__ which would give access, as a dictionary, to the 
parameters as supplied in the call, (or filled in by the defaults).

If this was accessible externally, as fn.__defaults__ is then examples 
such as:

 >    def a_method(self, cr, uid, ids, values, context=None):
 >      ...
 >      super(self, parent).a_method(cr, uid, ids, values, context=context)

would become:


     def a_method(self, cr, uid, ids, values, context=None):
       ...
       params = {k:v for k,v in __params__ if k in parent.a_method.keys()}
       # Possibly add some additional entries here!
       super(self, parent).a_method(**params)
-- 
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

---
This email has been checked for viruses by AVG.
https://www.avg.com



More information about the Python-ideas mailing list