Python Professional — Hard
Explanation
The output is "True then None" because the weak reference to the object is still valid when checked, but returns None after the object is deleted.
Ready to go further?
Related questions
- What is the purpose of Python's __init_subclass__ cls parameter's keyword arguments, and how are they passed?
- What does the following code output? import sys class MyList(list): def __sizeof__(self): return super().__sizeof__() + 100 ml = MyList([1, 2, 3]) print(sys.getsizeof(ml) - sys.getsizeof([1, 2, 3]))
- What does `itertools.chain(*iterables)` do?
- What does the following code output, and what pattern does it demonstrate? class Registry: _registry = {} def __init_subclass__(cls, key=None, **kwargs): super().__init_subclass__(**kwargs) if key: Registry._registry[key] = cls class Dog(Registry, key='dog'): pass class Cat(Registry, key='cat'): pass print(Registry._registry)
- What is the output of the following code? from typing import Protocol class Drawable(Protocol): def draw(self) -> None: ... class Circle: def draw(self) -> None: print('circle') def render(shape: Drawable) -> None: shape.draw() render(Circle())
- What does `@staticmethod` differ from `@classmethod` in Python?
