In the book D. Bizley "Python. Detailed reference "there is a judgment:

The fact that all objects in Python are first-class objects is often underestimated by novice programmers, whereas this allows you to write very compact and flexible programs. For example, suppose there is a text string, such as “GOOG, 100,490.10”, and you need to turn it into a list of fields, completing the necessary type conversions along the way. The following is a clever way to implement such a conversion: a list of types is created (which are also first-class objects) and several simple list processing operations are performed:

>>> line = "GOOG,100,490.10" >>> field_types = [str, int, float] >>> raw_fields = line.split(',') >>> fields = [ty(val) for ty,val in zip(field_types,raw_fields)] >>> fields 

['GOOG', 100, 490.10000000000002]

Are there any other useful "for beginner" examples of using this feature of the Python language?

And it seems that it is more like a source of puzzling mistakes.

  • I ask to describe the features of a language when teaching a language. - Vasyl Kolomiets
  • 2
    Practical example with functions: we go around directories and files recursively, depending on the path to the file, you need to process the file with a function. We use a piece of the path as a key in the dictionary, the function is the value in the dictionary. - insolor

1 answer 1

I think in this context, you can make some illustrative examples with functions that can be:

  • pass as arguments
  • to return
  • save to variable (as function reference)

Example:

Let us be given a two-dimensional matrix and we need to find some characteristics (minimum, maximum and average) for each row:

 In [15]: import random In [16]: A = [[random.randint(0, 10) for _ in range(10)] for _ in range(10)] In [17]: A Out[17]: [[6, 9, 6, 2, 0, 4, 3, 4, 5, 7], [2, 6, 6, 0, 8, 8, 6, 5, 2, 5], [10, 6, 0, 10, 1, 5, 8, 10, 9, 9], [3, 0, 4, 0, 0, 2, 1, 2, 8, 6], [5, 10, 8, 7, 8, 3, 0, 5, 0, 8], [9, 7, 2, 0, 8, 5, 0, 4, 4, 9], [9, 3, 8, 6, 6, 10, 5, 0, 0, 1], [7, 3, 2, 4, 5, 0, 0, 9, 0, 10], [8, 7, 10, 2, 6, 7, 2, 6, 2, 1], [3, 3, 1, 1, 1, 4, 8, 0, 3, 4]] In [18]: funcs = [min, max, lambda x: sum(x)/len(x)] In [19]: [[f(lst) for f in funcs] for lst in A] Out[19]: [[0, 9, 4.6], [0, 8, 4.8], [0, 10, 6.8], [0, 8, 2.6], [0, 10, 5.4], [0, 9, 4.8], [0, 10, 4.8], [0, 10, 4.0], [1, 10, 5.1], [0, 8, 2.8]] 

Now, in order to add new characteristics, for example, the sum of values ​​in each line, it will be enough to add the function name to the funcs list - the rest of the code remains the same:

 In [20]: funcs.append(sum) In [21]: [[f(lst) for f in funcs] for lst in A] Out[21]: [[0, 9, 4.6, 46], [0, 8, 4.8, 48], [0, 10, 6.8, 68], [0, 8, 2.6, 26], [0, 10, 5.4, 54], [0, 9, 4.8, 48], [0, 10, 4.8, 48], [0, 10, 4.0, 40], [1, 10, 5.1, 51], [0, 8, 2.8, 28]] 
  • And with the names of the modules something practical from this area can be? - Vasyl Kolomiets
  • 2
    @VasylKolomiets, for example, you can use the function reference from the required module depending on the fulfillment of conditions ... - MaxU