The lambda keyword in Python provides a shortcut for declaring small anonymous functions. Lambda functions behave just like regular functions declared with the def keyword. Example: >>> add = lambda x, y: x + y >>> add(5, 3) 8 It is equivalent to declaring the same add function with the def keyword (which would be slightly moer verbose):
2022-12-03
TL;DR Everything in Python is an object, including functions. You can assign them to variables store them in data structures pass or return them to and from ohter functions First-class functions allow you to abstract away and pass around behavior in your programs.
2022-12-03
The proper use of assertions is to inform developers about unrecoverable errors in a program. Assertions are not intended to signal expected error conditions. Assertions are meant to be internal self-checks for your program.
2022-12-03
In Decorator: Basics, we have seen the basics of decorators. import functools def decorator(func): @functools.wraps(func) def wrapper_decorator(*args, **kwargs): # Do something before value = func(*args, **kwargs) # Do something after return value return wrapper_decorator The key is to remember the following two expressions are equivalent (the latter uses the syntactic sugar):
2022-05-23
TL;DR Decorators define reusable building blocks you can apply to a callable to modify its behavior without permanently modifying the callable itself. The @ syntax is just a shorthand (syntax sugar) for calling the decorator on an input function.
2022-05-21
In Python, methods with __ (double underscore) prefix and suffix (e.g., __init__()) are special methods.They are called magic methods or dunder methods (dunder for “double underscore”). They can help override functionality for built-in functions for custom classes.
2022-05-04
The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don’t Repeat Yourself). Class A class is a blueprint for the object.
2022-05-04
2022-05-04
Python provides two built-in functions for sorting: list.sort(): modifies the list in-place sorted(): builds a new sorted list from an iterable Difference between these methods: list.sort() method is only defined for lists.
2022-05-02
2022-05-01