0

The baseoperator class in airflow has the pre_execute method, I saw a code like this:

subdag_operator_x.pre_execute = lambda a: api_x_call(a)

I can't found the name of this "=" sign over a class method, I mens, I want to understand what's this operation effect over the class method, (I tried finding, "override a method" or "overload a method", but no luck). It replaces the original method? can I use a common function instead of the lambda function?

It would be great if someone can point to some python documentation please.

Thanks

isoujiro
  • 51
  • 5
  • 1
    That is [a bad practice in Python](https://stackoverflow.com/questions/38381556/is-it-pythonic-naming-lambdas). Search there for `lambda`. – accdias Dec 03 '21 at 23:32
  • It is a `named lambda`, BTW. – accdias Dec 03 '21 at 23:36
  • @accdias it isn't really a bad *practice*, it just goes against the style, but since this is actually adding to an attribute, it could be acceptable – juanpa.arrivillaga Dec 03 '21 at 23:50
  • Yes, of course you can use a "common function", *there is only one type of function*! lambda expressions evaluate to regular function objects, a function definition statement creates such a function object and assigns it to the name in the statemnt `def (...): ...` So you can certainly do something like `def _pre_execute(a): return api_x_call(a)` then just do `subdag_operator_x.pre_execute = _pre_execute` – juanpa.arrivillaga Dec 03 '21 at 23:52
  • 2
    @juanpa.arrivillaga well, lambda is not even required here: `subdag_operator_x.pre_execute = api_x_call` – DeepSpace Dec 03 '21 at 23:53
  • 2
    @DeepSpace yes! I just thought about that as I was writing the comment above – juanpa.arrivillaga Dec 03 '21 at 23:53
  • This is a good point, @isoujiro usually when you you see `lambda : func()` you can just do `func`, not always, someone might want to force an argument, e.g. `lambda x: some_func(x, option='bar')` – juanpa.arrivillaga Dec 03 '21 at 23:55
  • Thanks to all, but my question was more related to the assignation of the lambda to a currently existing method definition, pre_execute is a method defined in baseoperator class, and this assignment is done in another python script that is using a baseoperator instance (so, subdag_operator_x is an instance of baseoperator). So, what's the meaning of assigning the lambda function to subdag_operator_x.pre_processing? (It replaces the definition of pre_processing in the original class?). – isoujiro Dec 04 '21 at 00:11

0 Answers0