Skip to content

Commit ce2dc13

Browse files
authored
added in Markdown for singledispathc() documentation (#7479)
* added in Markdown for singledispathc() documentation * Update singledispatch.md * Adding definition ---------
1 parent 9d08086 commit ce2dc13

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
Title: 'singledispatch()'
3+
Description: 'Turn a function into a single-dispatch generic function, allowing different implementations to be registered based on the type of the first argument.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Function'
9+
- 'Decorators'
10+
CatalogContent:
11+
- 'learn-python-3'
12+
- 'paths/data-science'
13+
---
14+
15+
In Python, **`functools.singledispatch`** is a [decorator](https://www.codecademy.com/resources/docs/python/decorators) that converts a [function](https://www.codecademy.com/resources/docs/python/functions) into a generic function(a type single function that can perform different actions depending on the data type of the input it receives) with behavior varying by the type of its first argument. It simplifies type-specific logic by allowing separate implementations to be registered instead of relying on multiple [if statements](https://www.codecademy.com/resources/docs/python/conditionals).
16+
17+
## Syntax
18+
19+
The `singledispatch()` function is included in the `functools` [module](https://www.codecademy.com/resources/docs/python/functools-module):
20+
21+
```py
22+
@functools.singledispatch
23+
def func(arg, ...):
24+
...
25+
```
26+
27+
**Parameters:**
28+
29+
- Takes a single callable (function) as the argument when used as a decorator.
30+
- The decorated function must accept at least one argument (the one whose type determines dispatch).
31+
32+
**Return value:**
33+
34+
- `singledispatch()` returns a single-dispatch generic function object.
35+
- Additional implementations can be registered for different types using the .register(type) method on the generic function.
36+
37+
## Example 1: Creat a generic function `process`
38+
39+
This example uses the `singledispatch` decorator to handle different input types in a custom way:
40+
41+
```py
42+
from functools import singledispatch
43+
44+
@singledispatch
45+
def process(value):
46+
print("Processing:", value)
47+
48+
@process.register(int)
49+
def _(value):
50+
print("Processing an integer:", value)
51+
52+
@process.register(str)
53+
def _(value):
54+
print("Processing a string:", value)
55+
56+
# Function calls
57+
process(10)
58+
process("Hello")
59+
```
60+
61+
Here is the output for this code:
62+
63+
```shell
64+
Processing an integer: 10
65+
Processing a string: Hello
66+
```
67+
68+
## Codebyte Example: Basic type-based greeting
69+
70+
This minimal example shows how `singledispatch()` changes behavior based on the type of its first argument:
71+
72+
```codebyte/python
73+
from functools import singledispatch
74+
75+
@singledispatch
76+
def greet(value):
77+
print("Hello!")
78+
79+
@greet.register(str)
80+
def _(value):
81+
print(f"Hello, {value}!")
82+
83+
greet("Alice")
84+
greet(42)
85+
```

0 commit comments

Comments
 (0)