Skip to content

Commit 40ca970

Browse files
authored
Merge pull request #93 from braboj/codex/add-header-to-dunder-methods-examples
Add explanatory headers to dunder examples
2 parents 0b3734b + 55223a2 commit 40ca970

11 files changed

Lines changed: 55 additions & 16 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dunder method examples
2+
# ---------------------------------------------------------------------------
3+
# Examples demonstrating how special methods customize object behavior

examples/15_dunder_methods/demo_iterator.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
"""
2-
* An iterable is an object that implements the __iter__ method which returns an iterator.
3-
* An iterator is an object that implements the __iter__ method which returns itself and the __next__ method which
4-
returns the next element.
5-
6-
https://www.pythontutorial.net/advanced-python/python-iterator-vs-iterable/
7-
8-
"""
1+
# Dunder methods and iteration
2+
# ---------------------------------------------------------------------------
3+
# Demonstrates how special methods customize iteration behavior.
4+
# * An iterable implements __iter__ returning an iterator.
5+
# * An iterator implements __iter__ returning itself and __next__ returning
6+
# the next element.
7+
# https://www.pythontutorial.net/advanced-python/python-iterator-vs-iterable/
98

109

1110
##################################################################################################

examples/15_dunder_methods/dunder_arithmetic_operators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for arithmetic operators
1+
# Dunder methods for arithmetic operators
2+
# ---------------------------------------------------------------------------
3+
# By defining special arithmetic methods, objects can participate in Python's
4+
# numeric operations. These dunder hooks let a class customize how instances
5+
# respond to +, -, *, and other operators.
26

37
class Complex(object):
48

examples/15_dunder_methods/dunder_attributes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for context manager
1+
# Dunder methods for attribute access
2+
# ---------------------------------------------------------------------------
3+
# Attribute related dunder methods such as __getattr__, __setattr__ and
4+
# __delattr__ give objects dynamic control over attribute access. They enable
5+
# customized lookup, assignment and deletion behavior.
26

37
class Complex(object):
48

examples/15_dunder_methods/dunder_builtin_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for object representation
1+
# Dunder methods for object representation
2+
# ---------------------------------------------------------------------------
3+
# Built-in type conversion functions like bool(), int() and bytes() call
4+
# corresponding dunder methods on objects. Implementing these hooks lets a
5+
# class define how it converts to fundamental Python types.
26

37
import struct
48
import math

examples/15_dunder_methods/dunder_context_manager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for context manager
1+
# Dunder methods for context manager
2+
# ---------------------------------------------------------------------------
3+
# The __enter__ and __exit__ methods allow an object to define its own
4+
# setup and teardown logic when used in a with-statement. This example shows
5+
# how implementing these hooks customizes resource management behavior.
26

37
class Complex(object):
48

examples/15_dunder_methods/dunder_customize_behavior.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for customizing object behavior
1+
# Dunder methods for customizing object behavior
2+
# ---------------------------------------------------------------------------
3+
# This script demonstrates several special methods that hook into object
4+
# creation, calling and destruction. Implementing these dunder methods lets a
5+
# class take control over how instances are created, invoked and cleaned up.
26

37
class TestClass(object):
48

examples/15_dunder_methods/dunder_iterator_protocol.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# Example: Dunder methods for the iterator protocol
1+
# Dunder methods for the iterator protocol
2+
# ---------------------------------------------------------------------------
3+
# Special (dunder) methods allow objects to integrate with Python features.
4+
# By implementing __iter__ and __next__, this class customizes iteration
5+
# behavior so that instances work seamlessly in for-loops and other iterable
6+
# contexts.
27

38
class Stack(object):
49

examples/15_dunder_methods/dunder_object_representation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for object representation
1+
# Dunder methods for object representation
2+
# ---------------------------------------------------------------------------
3+
# Special representation methods like __repr__ and __str__ let objects control
4+
# how they are displayed. This example highlights how these hooks customize the
5+
# string and byte output of a class.
26

37
import struct
48

examples/15_dunder_methods/dunder_operator_overloading.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Example: Dunder methods for operator overloading
1+
# Dunder methods for operator overloading
2+
# ---------------------------------------------------------------------------
3+
# Overloading arithmetic operators with dunder methods allows custom classes
4+
# to behave like built-in numeric types. The following Point class defines
5+
# how instances react to +, -, * and other operations.
26

37
class Point(object):
48

0 commit comments

Comments
 (0)