Skip to content
Premium

Python Decorators: Complete Guide & Examples

Go beyond basic functions into advanced Python patterns. Explore closures that maintain state across calls, decorators that add logging, timing, and caching without modifying source code, classmethod and staticmethod decorators, property decorators for computed attributes, functools.partial for function specialization, singledispatch for type-based dispatch, and practical production patterns for cleaner Python code.

Points to remember

  • Codes asked are relatively easier in comparison to SDE roles
  • Pythonic syntax is preferred in most companies
  • Focus is on optimized code
  • Start with the simplest solution and then improve
  • A lot of questions are repeated so mugging up the approaches help
  • Build intuition using Python Tutor

Finding the kth largest element in a list using sorting and index access

Explanation

  • The code initializes a list of integers and sets a value for k representing which largest element to find
  • It sorts the list in descending order using reverse=True parameter to arrange elements from highest to lowest
  • The kth largest element is accessed at index k-1 since list indices start at 0 (e.g., k=3 means third largest element)
  • This approach has O(n log n) time complexity due to sorting, where n is the length of the list
  • The solution prints the value at the calculated position which represents the desired kth largest item
python

Output

text

Python algorithm to determine if a list is sorted in ascending order

Explanation

  • The code initializes a boolean flag to True assuming the array is sorted
  • It iterates through the array comparing each element with the next one using a for loop
  • When any element is greater than its successor, the flag is set to False indicating unsorted order
  • After checking all adjacent pairs, the code prints "sorted" if flag remains True, otherwise prints "not sorted"
  • This approach has O(n) time complexity and checks elements sequentially from left to right
python

Output

text

This code snippet demonstrates how to find the maximum value in an array using iteration and built-in functions.

Explanation

  • Initializes a list L containing integer values.
  • Sets max_val to the first element of the list to start the comparison.
  • Iterates through each element in the list, updating max_val whenever a larger value is found.
  • Prints the maximum value found using the iterative method.
  • Also prints the maximum value using Python's built-in max() function for comparison.
python

Output

text

Identify the first element that appears a specified number of times in a list

Explanation

  • Initializes a list L containing integers and a variable k set to the desired occurrence count.
  • Creates an empty dictionary d to store the frequency of each element in the list.
  • Iterates through each element in the list, updating the count in the dictionary for each element.
  • After populating the dictionary, it checks for the first element that has a frequency equal to k.
  • Prints the first element that meets the condition and exits the loop immediately after finding it.
python

Output

text

Identifying and counting duplicate elements in a list using a dictionary

Explanation

  • Initializes a list L containing integers, some of which are duplicates.
  • Creates an empty dictionary d to store the count of each unique element.
  • Iterates through each element in the list; if the element is already in the dictionary, it increments its count, otherwise, it initializes the count to 1.
  • Finally, it iterates through the dictionary and prints elements that have a count greater than 1, indicating they are duplicates along with their counts.

Unlock the Full Article

You're reading a free 30% preview. The rest of this article is available to premium members.

Browse Free Posts

Already a premium member? Sign in here

Python Decorators: Complete Guide & Examples | Madhu Dadi