All fields

“Magic Methods & Operator Overloading” in Information Technology

38 questions across 1 technology.

Information Technology

38 questions in 1 technology

#QuestionTechnologyTopicLevel
1What are 'magic methods' (dunder methods) in Python, and why are they called that? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingBeginner
2How do comparison magic methods like __lt__, __le__, __gt__, and __ge__ support custom sorting? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
3How do context manager magic methods __enter__ and __exit__ work, and what does __exit__'s return value control? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingAdvanced
4How do __iadd__ and other in-place operator methods (__isub__, __imul__) differ from their non-in-place counterparts? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingAdvanced
5How does __slots__ interact with magic methods and instance attribute storage? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingAdvanced
6What is the difference between __str__ and __repr__? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingBeginner
7How do you overload arithmetic operators like + and - using __add__ and __sub__? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
8How does __contains__ let you customize the behavior of the 'in' operator? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
9How does __repr__ help with debugging when printing a list of custom objects? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingBeginner
10What do __eq__ and __hash__ need to satisfy together, and what happens if you only override __eq__? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
11How do __len__, __getitem__, and __setitem__ let a custom class act like a sequence or mapping? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
12How does __call__ let an instance of a class be invoked like a function? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
13How do you implement custom attribute access with __getattr__ versus __getattribute__? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingIntermediate
14What is the difference between __new__ and __init__, and when would you override __new__? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingAdvanced
15How does __bool__ (or the legacy __nonzero__) determine an object's truthiness, and what's the fallback if it's not defined? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingAdvanced
16What is a descriptor, and which dunder methods define the descriptor protocol? Python › Descriptors & PropertiesPythonDescriptors & PropertiesIntermediate
17How does functools.total_ordering reduce boilerplate for comparison methods? Python › functools & Functional Programming ToolsPythonfunctools & Functional Programming ToolsIntermediate
18How do you implement a custom __eq__ or __hash__ on a dataclass while still benefiting from its other auto-generated methods? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesAdvanced
19How does Python's own implementation of instance methods use the descriptor protocol to achieve automatic 'self' binding? Python › Descriptors & PropertiesPythonDescriptors & PropertiesAdvanced
20What does the @dataclass decorator automatically generate for a class? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesBeginner
21What does the @property decorator do, and how does it let a method be accessed like an attribute? Python › Descriptors & PropertiesPythonDescriptors & PropertiesBeginner
22What is the difference between an iterable and an iterator in Python? Python › Iterators & the Iterator ProtocolPythonIterators & the Iterator ProtocolBeginner
23What is multiple inheritance in Python, and how does it differ from single inheritance? Python › Multiple Inheritance & MROPythonMultiple Inheritance & MROBeginner
24Why must dictionary keys (and set elements) be hashable, and what makes an object hashable in Python? Python › Data Types & StructuresPythonData Types & StructuresIntermediate
25How do you make a dataclass immutable (read-only after creation) using frozen=True? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesIntermediate
26How do you write a decorator as a CLASS instead of a function, implementing __call__? Python › DecoratorsPythonDecoratorsIntermediate
27What is the difference between a data descriptor and a non-data descriptor, and how does this affect attribute lookup priority? Python › Descriptors & PropertiesPythonDescriptors & PropertiesIntermediate
28How does __set_name__ let a descriptor automatically learn the attribute name it's being assigned to on the owning class? Python › Descriptors & PropertiesPythonDescriptors & PropertiesIntermediate
29How does @functools.wraps preserve metadata when writing decorators? Python › functools & Functional Programming ToolsPythonfunctools & Functional Programming ToolsIntermediate
30How does Python know whether to treat a for-in target as iterable, and what error occurs if it isn't? Python › Iterators & the Iterator ProtocolPythonIterators & the Iterator ProtocolIntermediate
31How does the built-in `object` class relate to every class's MRO, and why does it always appear last? Python › Multiple Inheritance & MROPythonMultiple Inheritance & MROIntermediate
32How does frozenset differ from a regular set, and what specific use case does its immutability enable? Python › Data Types & StructuresPythonData Types & StructuresAdvanced
33How do you use dataclasses.field(compare=False) or field(repr=False) to exclude a specific field from the auto-generated __eq__ or __repr__? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesAdvanced
34How do you implement custom ordering (<, <=, >, >=) for a dataclass using order=True, and what constraint does it place on field comparison? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesAdvanced
35How would you use dataclasses.replace() to create a modified copy of an immutable (frozen) dataclass instance? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesAdvanced
36When would you choose a NamedTuple over a @dataclass, and vice versa, given their overlapping use cases? Python › Dataclasses & NamedTuplesPythonDataclasses & NamedTuplesAdvanced
37How does __class_getitem__ enable generic-looking syntax like MyClass[int]? Python › Metaclasses & Class CustomizationPythonMetaclasses & Class CustomizationAdvanced
38How does __instancecheck__ and __subclasscheck__ on a metaclass enable 'virtual subclassing' as used by collections.abc? Python › Metaclasses & Class CustomizationPythonMetaclasses & Class CustomizationAdvanced