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

Jonathan Fine jfine2358 at gmail.com
Sat Sep 8 14:12:55 EDT 2018


The problem is to improve
    https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1477-L1493

Here's a suggestion. For each of four permissions, the code has an
assignment such as

    has_add_permission = inline._has_add_permission(request, obj)

and the function call has four named parameters such as

    has_add_permission=has_add_permission

where the permissions are add, change, deleted and view.

Browsing the file, I see something similar in several places, such as

<code>
https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1136-L1139

'has_view_permission': self.has_view_permission(request, obj),
'has_add_permission': self.has_add_permission(request),
'has_change_permission': self.has_change_permission(request, obj),
'has_delete_permission': self.has_delete_permission(request, obj),
</code>

So, off the top of my head, have a function

    get_permissions(item, request, obj)

and then simply write

    ** get_permissions(item, request, obj)

in the function call, to pass the permissions to the called function.
By the way, for ease of use this is relying on

    https://www.python.org/dev/peps/pep-0448/ # Additional Unpacking
Generalizations

which allows multiple **kwargs in a function call. It was implemented
in Python 3.5.

By the way, Django 1.11 and 2.0 are still supported by Django, and
they both support Python 3.4. So we'll have to wait for a bit before
Django could accept this suggestion.

    https://docs.djangoproject.com/en/2.1/faq/install/#faq-python-version-support

-- 
Jonathan


More information about the Python-ideas mailing list