Is it bad style to reassign long variables as a local abbreviation?
Question: I prefer to use long identifiers to keep my code semantically clear, but in
the case of repeated references to the same identifier, I'd like for it to
"get out of the way" in the current scope. Take this example in Python:

    
    
    def define_many_mappings_1(self):
        self.define_bidirectional_parameter_mapping("status", "current_status")
        self.define_bidirectional_parameter_mapping("id", "unique_id")
        self.define_bidirectional_parameter_mapping("location", "coordinates")
        #etc...
    

Let's assume that I really want to stick with this long method name, and that
these arguments are always going to be hard-coded. Implementation 1 feels
wrong because most of each line is taken up with a repetition of characters.
The lines are also rather long in general, and will exceed 80 characters
easily when nested inside of a class definition and/or a try/except block,
resulting in ugly line wrapping. Let's try using a for loop:

    
    
    def define_many_mappings_2(self):
        mappings = [("status", "current_status"),
                    ("id", "unique_id"),
                    ("location", "coordinates")]
        for mapping in mappings:
            self.define_parameter_mapping(*mapping)
    

I'm going to lump together all similar iterative techniques under the umbrella
of Implementation 2, which has the improvement of separating the "unique"
arguments from the "repeated" method name. However, I dislike that this has
the effect of placing the arguments before the method they're being passed
into, which is confusing. I would prefer to retain the "verb followed by
direct object" syntax.

I've found myself using the following as a compromise:

    
    
    def define_many_mappings_3(self):
        d = self.define_bidirectional_parameter_mapping
        d("status", "current_status")
        d("id", "unique_id")
        d("location", "coordinates")
    

In Implementation 3, the long method is aliased by an extremely short
"abbreviation" variable. I like this approach because it is immediately
recognizable as a set of repeated method calls on first glance while having
less redundant characters and much shorter lines. The drawback is the usage of
an extremely short and semantically unclear identifier "d".

What is the most readable solution? Is the usage of an "abbreviation variable"
acceptable if it is explicitly assigned from an unabbreviated version in the
local scope?


Answer: itertools to the rescue again! Try using starmap - here's a simple demo:

    
    
    list(itertools.starmap(min,[(1,2),(2,2),(3,2)]))
    

prints

    
    
    [1,2,2]
    

starmap is a generator, so to actually invoke the methods, you have to consume
the generator with a list.

    
    
    import itertools
    
    def define_many_mappings_4(self):    
        list(itertools.starmap(
            self.define_parameter_mapping,
            [
                ("status", "current_status"),
                ("id", "unique_id"),
                ("location", "coordinates"),
            ] ))
    

Normally I'm not a fan of using a dummy list construction to invoke a sequence
of functions, but this arrangement seems to address most of your concerns.

If `define_parameter_mapping` returns None, then you can replace `list` with
`any`, and then all of the function calls will get made, and you won't have to
construct that dummy list.

