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?

3
  • 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. Commented Jul 15, 2022 at 15:38
  • See also: Factory (object-oriented programming) Commented Jul 15, 2022 at 15:41
  • @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. Commented Jul 15, 2022 at 16:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comprehensive answer! Especially for the hint to functoools.partial().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.