I recently had a Python interview with EPAM and wanted to share the questions in case it helps anyone:
def decorator1(func):
def wrapper(*args, **kwargs):
print("Decorator 1: Before function call")
result = func(*args, **kwargs)
print("Decorator 1: After function call")
return result
return wrapper
def decorator2(func):
def wrapper(*args, **kwargs):
print("Decorator 2: Before function call")
result = func(*args, **kwargs)
print("Decorator 2: After function call")
return result
return wrapper
@decorator1
@decorator2
def my_function():
print("Inside my_function")
my_function() try:
# Code that might raise exceptions
value = int("abc") # This will raise a ValueError
result = 10 / 0 # This will raise a ZeroDivisionError
except ValueError as e:
print(f"Invalid input: {e}")
# Specific handling for ValueError
except ZeroDivisionError as e:
print(f"Cannot divide by zero: {e}")
# Specific handling for ZeroDivisionError
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Catch-all for any other exceptions (use with caution)def demo_no_catch():
try:
raise Exception('general exceptions not caught by specific handling')
except ValueError as e:
raise Exception from eclass ParentA:
def common_method(self):
print("Method from ParentA")
class ParentB:
def common_method(self):
print("Method from ParentB")
class Child(ParentA, ParentB):
pass
child_obj = Child()
child_obj.common_method()#EPAM #Python