0

I have come across some code that uses lambda expressions on multiple occasions to pass around classes.

class Clazz:
    def __init__(self, a, b=-1):
        self.a = a
        self.b = b

    def __str__(self):
        return f'Clazz: {self.a}; {self.b}'

clazz_fn1 = lambda a: Clazz(a)  # o1_fn is than passed to some other function ...
c1 = clazz_fn1(1)               # ... where the Clazz object is initialized
print(c1)

Is there any advantage in using a the lambda keyword over just passing the class name?

clazz_fn2 = Clazz
c2 = clazz_fn2(2)
print(c2)

The only advantage that comes to my mind, is that lambda functions offer the possibility to pass further arguments along:

clazz_fn3 = lambda a: Clazz(a, b=42)
c3 = clazz_fn3(3)
print(c3)

On the other hand, it is my understanding that using lambda functions is not something that is generally recommended (see e.g. this posts: Guido van Rossum interview, post on overusing of lambda expressions... still this discussion seems to be mostly in favour of lambda expressions).

I am also aware of the question on why to use lambda functions, but using lambda functions to pass around classes as described above does not seem to be the typical use case for the lambda syntax that is covered by this more general discussion.

So are there further differences between the two options or is this a purely stylistic choice?

rosa b.
  • 1,759
  • 2
  • 15
  • 18
  • If you're not satisfied with the simple, normal and clear `c1 = Clazz(1)`, you might create a normal function rather than using a less readable lambda. – Thierry Lathuille Jul 15 '22 at 15:38
  • See also: [Factory (object-oriented programming)](https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)) – 001 Jul 15 '22 at 15:41
  • @[Thierry Lathuille](https://stackoverflow.com/users/550094/thierry-lathuille) Thanks for your comment. I am fully satisfied with using a normal object initialisation or function for that purpose. :) Just wondering why one might prefer a lambda expression here. – rosa b. Jul 15 '22 at 16:00

1 Answers1

2

Is there any advantage in using a the lambda keyword over just passing the class name?

Not really. In the simple case you show, you are just adding a (IMHO unneeded) level of indirection to the instantiation process. Using the class name itself would be simpler and more comprehensible.

The only advantage that comes to my mind, is that lambda functions offer the possibility to pass further arguments along:

Yeah, that's a good use case. In fact, it's such a common use case that functools.partial() was invented exactly for this purpose:

from functools import partial

clazz_fn3 = partial(Clazz, b=42)
c3 = clazz_fn3(3)
print(c3)

So are there further differences between the two options or is this a purely stylistic choice?

I'd say this is most definitely a stylistic choice of the programmer. Other than the obvious difference in the slightly more (unneeded) overhead when wrapping using a lambda, there isn't much else of objective relevance that'd make one of the two versions preferable over the other.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128