An example where lambdas are very useful in Python

I met lambda expressions in Python . I have already seen many simple examples (including examples on SE), where lambda expressions create functions such as adders, but I still do not see their real usefulness, for example. in some example, where it would be painful to write the same world of code without lambda.

Could you show something for word processing where you would use lambda expressions and it would be difficult to avoid lambda expressions? But something practical (not a math game).

+4
source share
7 answers

In this case, it would be painful to write all lambda expressions as a separate function.

What does this code briefly do? Converts a custom excel table into an insert statement for a custom database table. There is a mapping of excel field fields and database fields, as well as a mapping between excel fields and functions that must be applied to an excel table value before it is inserted into db. You really do not want to define a separate function for each field.

 map_func = { 'ID' : lambda x : 'mig_farm_seq.nextval', 'ENTERPRISE_NAME' : wrap_str, 'TAX_NUMBER' : wrap_str, 'FAMILY_NAME' : lambda x : wrap_str(x.split()[0]), 'GIVEN_NAME' : lambda x : wrap_str(x.split()[1]), 'ENTERPRISE_REGISTRATION_NUMBER' : wrap_str, 'PREMISE_NAME' : wrap_str, 'HOUSE_ID' : wrap_str, 'POSTAL_CODE' : wrap_str, 'PHONE_NUMBER_1' : lambda x : wrap_str(get_phone_number(x, True)), 'PHONE_NUMBER_2' : lambda x : wrap_str(get_phone_number(x, False)), 'FAX_NUMBER' : lambda x : wrap_str(x.replace(' ', '')), 'BANK_IDENTIFIER' : lambda x : wrap_str(x.replace(' ', '').replace('-', '')[:3]), 'BANK_ACCOUNT_NUMBER' : lambda x : wrap_str(x.replace(' ', '').replace('-', '')), 'NUMBER_OF_EMPLOYEES' : wrap_null, 'SETTLEMENT_NUMBER' : wrap_null, 'REGISTRATION_NUMBER' : lambda x : insert_reg_number % x, 'GENDER' : wrap_str, 'ACTIVITY' : lambda x : '0', 'REG_HOLDER_ACTIVITY' : lambda x : '0', 'PROCESSED_BY_JOB' : lambda x : '0' } 

source: http://pastebin.com/MxEPBMaZ

+5
source

In one place, I often use them: the key function of the sort and sorted :

 >>> person = lambda name, age: { "name": name, "age": age } >>> people = [ person("Joe", 61), person("Jane", 52) ] >>> people.sort(key=lambda person: person["age"]) 

This will sort the list of people by age.

Another place I use lambdas is re.sub :

 >>> re.sub("0x([0-9a-f]+)", lambda match: "0x" + match.group(1).upper(), "0xfa") '0xFA' 
+4
source

Instead of offering anything here, I would recommend visiting the lib directory of your Python installation and grep for lambda . You will have enough examples to satisfy your appetite.

+2
source

Since you asked about text processing, look at this example (and this one is similar).

Closures are almost always easier to handle than objects (since the environment is captured implicitly), but for those who think in terms of OOP, this is not so obvious. I would recommend learning at least one decent functional language (lambdas in python are too limited), and thus you will understand how to effectively apply these methods in languages ​​like Python.

+1
source

They are useful when you need a short function to compare two elements or perform some other operation, and this will be passed as an argument, and there is no need for the function to have a name because it is used only as an argument.

They are useful when a function written as a lambda is shorter than the def line you usually used to call it, and the return you would use at the end.

They are useful when you want to partially apply a function β€” you have a function that you need to pass somewhere else, and you want to provide some or all of the arguments to the passed function without calling it. Lamdda allows you to do this without having to define and name a separate function for each change you need.

0
source

All my Django projects include 2 lines (settings.py):

 DIRNAME = os.path.dirname(__file__) _rel = lambda x: os.path.join(DIRNAME, x) 
0
source

One case when they are really useful:

Suppose you are using PyQt, which allows you to connect signals to other slots (methods). Sometimes I just need to perform a certain action rather than write a function, so all I need is a drop function.

 obj.signal.connect(lambda: doFoo(bar)) 

Of course, this contrasts with:

 def doBoo(): return doFoo(bar) obj.signal.connect(doBoo) 

.. Significantly clear the old path.

0
source

Source: https://habr.com/ru/post/1385381/


All Articles