WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Advanced · question 53 of 100

How do you use Python’s built-in decorators, such as @property, @staticmethod, and @classmethod? Provide examples.?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Python decorators are a way to modify or enhance the behavior of functions or classes in a clean and simple way. They are denoted by the "@" symbol followed by the name of the decorator. In this answer, we’ll cover three built-in decorators in Python: @property, @staticmethod, and @classmethod.

@property is a decorator that allows you to define a method as a read-only property. It’s commonly used to provide a getter method for a class attribute. Here’s an example:

    class Person:
        def __init__(self, name):
        self._name = name
    
    @property
    def name(self):
        return self._name
    
    p = Person("John")
    print(p.name)  # Output: "John"

In this example, we define a Person class with a private _name attribute. We then define a name property using the @property decorator. This allows us to retrieve the value of _name using the p.name syntax.

@staticmethod is a decorator that allows you to define a method as a static method, which means it can be called without an instance of the class. Here’s an example:

    class MathUtils:
        @staticmethod
        def add(x, y):
            return x + y

    print(MathUtils.add(2, 3))  \# Output: 5

In this example, we define a MathUtils class with a add method that’s defined as a static method using the @staticmethod decorator. This allows us to call the add method using the class name rather than an instance of the class.

@classmethod is a decorator that allows you to define a method as a class method, which means it takes the class as its first argument instead of an instance of the class. Here’s an example:

    class Person:
        count = 0
        
        def __init__(self, name):
            self.name = name
            Person.count += 1
        
        @classmethod
            def num_people(cls):
            return cls.count
    
    p1 = Person("John")
    p2 = Person("Jane")
    print(Person.num_people())  # Output: 2

In this example, we define a Person class with a num_people method that’s defined as a class method using the @classmethod decorator. This allows us to access the class attribute count using the cls parameter, which is a reference to the class itself. This method returns the total number of people created so far.

In summary, Python decorators are a powerful way to modify the behavior of functions or classes. The @property, @staticmethod, and @classmethod decorators are built-in decorators in Python that provide useful functionality for defining read-only properties, static methods, and class methods, respectively.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics