Python Professional — Hard
Ready to go further?
Related questions
- What is the output of the following code? def decorator(cls): original_init = cls.__init__ def new_init(self, *args, **kwargs): original_init(self, *args, **kwargs) self.decorated = True cls.__init__ = new_init return cls @decorator class Foo: def __init__(self): self.value = 1 f = Foo() print(f.value, f.decorated)
- What is a Python decorator?
- What is the purpose of __fspath__ and os.fspath() in Python?
- What does the following code output? from itertools import groupby data = [('a', 1), ('a', 2), ('b', 3), ('a', 4)] for key, group in groupby(data, key=lambda x: x[0]): print(key, list(group))
- What is the output of the following code? class Meta(type): def __new__(mcs, name, bases, namespace): namespace['class_id'] = name.upper() return super().__new__(mcs, name, bases, namespace) class Foo(metaclass=Meta): pass print(Foo.class_id)
- What does the following code output? from typing import TypeVar, Generic T = TypeVar('T') class Stack(Generic[T]): def __init__(self): self._items: list[T] = [] def push(self, item: T) -> None: self._items.append(item) def pop(self) -> T: return self._items.pop() s: Stack[int] = Stack() s.push('not an int') print(s.pop())
