[Python-ideas] Improving fn(arg=arg, name=name, wibble=wibble) code

Jonathan Fine jfine2358 at gmail.com
Thu Sep 13 15:34:41 EDT 2018


Hi Anders and Michael

I think it would be great if plugging existing functions into our code
was as easy as, well, plugging an electrical appliance into a wall
socket. However, even this ease of use has taken time.

    https://en.wikipedia.org/wiki/AC_power_plugs_and_sockets#History

And travellers know that adapters are required. And for some
equipment, non-trivial transformers.

Even though there's been much progress, such as multiple inheritance,
super() and iterators, there's still nuisance, pain and errors.The
good news is that we've identified some ways of improving the
situation.

Although there are the usual contending views and differences of
opinion, there's also positive energy and shared values. So I'm
optimistic.

Anders, you wrote:

> I’ll repeat myself: what about .format()? If you localize you can’t use
> f-strings. What about templates in web apps? Obviously f-strings won’t do.
> What about json blobs in REST APIs? Again no help from f-strings. What about
> functions with more than 3 arguments generally?

For f-strings, can't we turn the dictionary into a namespace, like so

Python 3.6.2 (default, Jul 29 2017, 00:00:00)

    >>> d = dict(a='apple', b='banana', c='cherry', d=137)
    >>> class A: pass

    >>> ns = A()
    >>> ns.__dict__ = d # Yes, we can do this!

    >>> ns.a
    'apple'

    >>> f'The a-fruit is {ns.a}, and d is {ns.d}.'
   'The a-fruit is apple, and d is 137.'

I don't see why this doesn't help reduce the pain.

For functions with more than 3 arguments, perhaps you could give some
examples where you'd like improvement.

best wishes

Jonathan


More information about the Python-ideas mailing list