[Python-ideas] Why operators are useful

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Mon Mar 18 19:34:48 EDT 2019


On 3/18/19 6:08 PM, Steven D'Aprano wrote:
> On Mon, Mar 18, 2019 at 03:12:52PM +0100, Antoine Pitrou wrote:
> 
>> (also, don't forget you can still use the copy() + update() method)
> 
> If we had fluent method calls, we could write:
> 
>      process(mapping.copy().update(other))
> 
> but we don't, so we use a pointless temporary variable:
> 
>      temp = mapping.copy()
>      temp.update(other)
>      process(temp)
>      del temp  # don't pollute the global namespace
> 
> 
> turning what ought to be a simple expression into an extra two or three
> statements.

So how many of you got tired of those three statements and
added something like the following function to your private
collection of useful functions:

     def merged_mappings(mapping, other):
         temp = mapping.copy()
         temp.update(other)
         return temp # no need to del temp here!

turning two or three statements into a simple expression?

I sure didn't.


More information about the Python-ideas mailing list