วันอาทิตย์ที่ 18 กุมภาพันธ์ พ.ศ. 2567

What is the purpose of the @staticmethod decorator in Python, and how does it differ from the @classmethod decorator? Provide an example to illustrate their usage.

 Q31: What is the purpose of the @staticmethod decorator in Python, and how does it differ from the @classmethod decorator? Provide an example to illustrate their usage.

A31:

  • Purpose of @staticmethod and @classmethod:

    • The @staticmethod decorator in Python is used to define a static method within a class. Static methods are associated with the class rather than instances and do not have access to instance-specific data.

    • The @classmethod decorator is used to define a class method within a class. Class methods take the class itself as the first parameter, allowing them to work with class-level data.

  • Difference and Example:

    • @staticmethod does not take the instance or class as its first parameter, making it independent of instance or class-specific data.

    • @classmethod takes the class as its first parameter, allowing access to class-specific data.

    class MathOperations: @staticmethod def add(x, y): return x + y @classmethod def multiply(cls, x, y): return x * y # Using static method result_add = MathOperations.add(3, 5) # Using class method result_multiply = MathOperations.multiply(3, 5)

    In this example, add is a static method, and multiply is a class method. The add method does not have access to the instance or class, while the multiply method can access class-specific data through the cls parameter.

How does the Python unittest module facilitate unit testing, and what are the key components involved in writing and executing tests using this module?

Q30: How does the Python unittest module facilitate unit testing, and what are the key components involved in writing and executing tests using this module?

A30:

  • Facilitating Unit Testing with unittest:

    • The unittest module in Python provides a framework for writing and running unit tests. It is inspired by the testing frameworks in other programming languages and follows the xUnit style.
  • Key Components in Writing and Executing Tests:

    • Test Case Class: Define a class that inherits from unittest.TestCase to create a test case. Each test method within this class should start with the word "test."
    import unittest class MyTestCase(unittest.TestCase): def test_addition(self): result = 2 + 3 self.assertEqual(result, 5)

Test Runner: Execute tests using a test runner. The unittest module provides a built-in test runner that can be invoked from the command line.
python -m unittest my_module.py
    Assertion Methods: Use assertion methods provided by TestCase to check whether the actual results match the expected results. Common assertions include assertEqual, assertTrue, assertFalse, etc.

    class MyTestCase(unittest.TestCase): def test_addition(self): result = 2 + 3 self.assertEqual(result, 5)


    Test Discovery: Automatically discover and run tests using the unittest test discovery mechanism. This allows you to organize tests across multiple files and directories.

    python -m unittest discover -s tests


    The unittest module simplifies the process of writing, organizing, and executing unit tests, promoting good testing practices in Python projects.





    What is the purpose of the __call__ method in Python classes, and how does it allow instances to be callable? Provide an example to illustrate its usage

     Q29: What is the purpose of the __call__ method in Python classes, and how does it allow instances to be callable? Provide an example to illustrate its usage.

    A29:

    • Purpose of __call__ Method:

      • The __call__ method in Python classes allows instances of a class to be callable as if they were functions. When an object is called as a function, the __call__ method is invoked.
    • Illustration of Usage:

      • By defining the __call__ method, a class instance can exhibit behavior similar to a function, making the instance itself callable.
      class CallableClass: def __init__(self): self.counter = 0 def __call__(self): self.counter += 1 return f"Instance called. Counter: {self.counter}" # Creating an instance obj = CallableClass() # Calling the instance result1 = obj() result2 = obj() print(result1) print(result2)

      In this example, CallableClass has a __call__ method that increments a counter each time the instance is called. The instance obj is callable, and calling it modifies the counter and returns a message.


    How does the Python Global Interpreter Lock (GIL) impact the performance of multi-threaded programs, and what strategies can be employed to mitigate its effects?

      Q10: How does the Python Global Interpreter Lock (GIL) impact the performance of multi-threaded programs, and what strategies can be emplo...