OK, quick search reveals some code in my actual codebase that illustrates
the concept.  This isn't code I was actually looking at this week, but it
is in one of the company repos.

def form_to_kwarg(form):
    """Converts the form into the kwarg used in creates and updates.

    If the form isn't validated, returns an empty dictionary.
    """
    if form.validate():
        attrs_kwarg = {}
        keys = get_column_names()
        keys.remove('cost')
        keys.remove('id')
        keys.append('client_id')
        keys.append('ratelimit')
        for attr in keys:
            attrs_kwarg[attr] = getattr(form, attr).data
        attrs_kwarg['measurements'] = slugs_to_string(attrs_kwarg[
'measurements'])
        return attrs_kwarg
    else:
        return None


This put a certain thumb on the scale since I searched for .append() which
will automatically only find lists.  Or maybe it's a deque. Or something
else. I don't actually know for sure (but I kinda think the programmers who
did this wouldn't have used something more "exotic").

So who knows what `get_column_names()` gives me.  I can see on the next
line that we remove one of them.  But that could be either a set or a list
(or some other things).  Then there are the .append()s that give it away...
but what if those hadn't been there and I just discovered the need to
"just_add_it()" to address the current issue?

So then I loop through this 'keys' thing, whatever it is.  That could be
any iterable at that point, but in particular any collection.  Indeed,
there *is* a determinate answer to what kind of thing keys is.  But while
I'm looking at just this function, I might well not know which type that
is... and usually I don't want to think about it.  And maybe the
implementation of `get_column_names()` will change later to use a different
collection.  I shouldn't need to worry about that.

In this example, moreover, it sure looks like all the items inside the
collection are immutable (strings), and it sure does suggest that they each
occur once but that I don't care about the order (since I'm using those
items as keys to a new dictionary, and duplicates would likely cause bugs).

Btw, yes I also see that the docstring doesn't match the behavior :-).

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OA24L6RPWOHMQQC7WFB53IFUTITCIVGA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to