Decorators & Lambda Functions in Python - 50
Q&A;
Q1. What is a decorator in Python?
Ans: A decorator is a function that modifies the behavior of another function or class without
changing its code.
Q2. What is the purpose of decorators?
Ans: They allow reusability, cleaner code, and separation of concerns like logging, authentication,
etc.
Q3. How do you define a decorator?
Ans: A decorator is defined as a function that takes another function as input and returns a new
function.
Q4. What is the syntax for applying a decorator?
Ans: Use the @ symbol above a function definition. Example: @decorator_name.
Q5. Can you use multiple decorators on one function?
Ans: Yes, multiple decorators can be stacked on a function.
Q6. What are built-in decorators in Python?
Ans: Examples are @staticmethod, @classmethod, and @property.
Q7. What is @staticmethod used for?
Ans: It defines a method that does not require an instance or class reference.
Q8. What is @classmethod used for?
Ans: It defines a method that takes `cls` as the first argument instead of `self`. It works with
class-level data.
Q9. What is @property decorator?
Ans: It allows methods to be accessed like attributes, providing getter/setter functionality.
Q10. Can decorators take arguments?
Ans: Yes, you can create decorators that accept arguments by nesting functions.
Q11. What is functools.wraps used for in decorators?
Ans: It preserves metadata (name, docstring) of the original function when decorated.
Q12. What is a higher-order function?
Ans: A function that takes another function as an argument or returns a function. Decorators are
higher-order functions.
Q13. Can we decorate class methods?
Ans: Yes, decorators can be applied to instance methods, static methods, or class methods.
Q14. What is a use case of decorators?
Ans: They are used in logging, access control, memoization, and performance measurement.
Q15. Can decorators return values?
Ans: Yes, the decorated function can return values normally, which are handled by the wrapper
function.
Q16. What is the difference between function decorators and
class decorators?
Ans: Function decorators modify functions; class decorators modify classes.
Q17. Can you create a class-based decorator?
Ans: Yes, by defining the __call__ method in a class, making it callable as a decorator.
Q18. What is a lambda function in Python?
Ans: A lambda function is an anonymous, one-line function defined using the `lambda` keyword.
Q19. What is the syntax of a lambda function?
Ans: lambda arguments: expression.
Q20. What are the advantages of lambda functions?
Ans: They allow quick, inline, throwaway functions without defining a full function.
Q21. Can lambda functions have multiple arguments?
Ans: Yes, lambda can take multiple arguments, but only one expression.
Q22. Can lambda functions have multiple statements?
Ans: No, lambda functions can only have a single expression.
Q23. How do you use lambda with map()?
Ans: map(lambda x: x*2, [1,2,3]) returns [2,4,6].
Q24. How do you use lambda with filter()?
Ans: filter(lambda x: x%2==0, [1,2,3,4]) returns [2,4].
Q25. How do you use lambda with reduce()?
Ans: reduce(lambda x,y: x+y, [1,2,3,4]) returns 10.
Q26. Can lambda functions return multiple values?
Ans: No, they return a single value, but that value can be a tuple or list.
Q27. What is the scope of lambda functions?
Ans: They follow the same scope rules as normal functions.
Q28. Are lambda functions faster than normal functions?
Ans: No, they are not inherently faster, but they are concise.
Q29. Can you assign a lambda function to a variable?
Ans: Yes. Example: add = lambda x,y: x+y.
Q30. Can a lambda function call another function?
Ans: Yes, as long as the function is in scope.
Q31. What is the difference between def and lambda?
Ans: def creates named functions with multiple statements, while lambda creates small,
anonymous, single-expression functions.
Q32. Can you use conditional expressions inside a lambda?
Ans: Yes. Example: lambda x: 'Even' if x%2==0 else 'Odd'.
Q33. What happens if a lambda function has no arguments?
Ans: It behaves like a function with no parameters. Example: lambda: 10 returns 10.
Q34. Can you use *args and **kwargs in lambda functions?
Ans: Yes, they can accept variable-length arguments.
Q35. What is a real-world use case of lambda functions?
Ans: They are often used in sorting, callbacks, and functional programming with map/filter/reduce.
Q36. How do you sort a list using lambda?
Ans: sorted(data, key=lambda x: x[1]) sorts by the second element.
Q37. Can you use lambda with dictionaries?
Ans: Yes, often used for sorting dictionary items or in dictionary comprehensions.
Q38. What is a nested lambda function?
Ans: A lambda function inside another lambda function.
Q39. Can lambda functions be recursive?
Ans: Not directly, but possible if assigned to a variable before calling.
Q40. What is the difference between lambda and inline def?
Ans: Inline def isn’t valid in Python, so lambda is used for anonymous inline functions.
Q41. How do decorators and lambda functions differ?
Ans: Decorators modify existing functions, while lambda functions create anonymous functions.
Q42. Can you use decorators with lambda functions?
Ans: Not directly, since lambdas have no name, but you can assign them to a variable and decorate
that.
Q43. What happens if you pass a lambda to a decorator?
Ans: It will work if the decorator expects a callable function.
Q44. Can a decorator return a lambda function?
Ans: Yes, decorators can return lambdas just like normal functions.
Q45. What is the main limitation of lambda functions?
Ans: They can only contain a single expression and no statements.
Q46. Why use lambda instead of def?
Ans: For short, simple, throwaway functions where defining a full function is unnecessary.
Q47. Can a lambda function contain try-except?
Ans: No, because try-except is a statement, not an expression.
Q48. Can decorators be nested?
Ans: Yes, one decorator can wrap another, forming a chain of modifications.
Q49. What is memoization using decorators?
Ans: It is caching results of expensive function calls using a decorator.
Q50. What is the difference between anonymous (lambda) and
named functions?
Ans: Anonymous functions have no name, while named functions are defined using def with an
identifier.