You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/enums/about.md
+32-16Lines changed: 32 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,31 @@
1
1
# About
2
2
3
-
In Python, [an enum][enum-docs] is a set of unique names that are bound unique, **constant** values. Enums are defined by inheriting an `Enum` class. Built-in enum types are available in the module `enum` and the class `Enum` can be imported using `from enum import Enum`.
3
+
In Python, [an enum][enum-docs] is a set of unique names that are bound unique, **constant** values.
4
+
`Enums` are defined by inheriting an `Enum` class.
5
+
Built-in enum types are available in the module `enum` and the class `Enum` can be imported using `from enum import Enum`.
6
+
4
7
5
8
```python
9
+
from enum import Enum
10
+
6
11
classColor(Enum):
7
12
RED=1
8
13
GREEN=2
9
14
```
10
15
11
-
Note that the values of the enum members can be any data types such as str, tuple, float, etc.
16
+
Note that the values of the `enum` members can be any data types such as `str`, `tuple`, `float`, etc.
12
17
13
18
```python
14
19
classColor(Enum):
15
20
RED='red'
16
21
GREEN='green'
17
22
```
18
23
19
-
Enums can also be created via the following [functional API][enum-functional-api].
24
+
`Enums` can also be created using [function-call syntax][enum-functional-example].
When assigning the same value to two members in an enum, the latter assigned member will be an alias to the formed one. It is not allowed to use the same name for two members of an enum.
37
+
When assigning the same value to two members in an `enum`, the latter assigned member will be an alias to the former one. It is not allowed to use the same name for two different members of an `enum`.
31
38
32
39
```python
33
40
classColor(Enum):
@@ -50,12 +57,13 @@ for member in Color:
50
57
# __members__.items() helps you to loop through alias as well
51
58
for member in Color.__members__.items():
52
59
print(member)
53
-
#=>('RED', <Color.RED: 1>)
54
-
#=>('GREEN', <Color.GREEN: 2>)
55
-
#=>('ALIAS_OF_RED', <Color.RED: 1>)
60
+
#=>('RED', <Color.RED: 1>)
61
+
#=>('GREEN', <Color.GREEN: 2>)
62
+
#=>('ALIAS_OF_RED', <Color.RED: 1>)
56
63
```
57
64
58
-
Enum members can be compared using [`is` (_identity operator_)][identity-keyword] or `is not`. The `==` or `!=` (_equality operators_) work likewise.
65
+
`Enum` members can be compared using [`is` (_identity operator_)][identity-keyword] or `is not`. The `==` or `!=` (_equality operators_) work likewise:
66
+
59
67
60
68
```python
61
69
a = Color.RED
@@ -76,18 +84,18 @@ class Shape(Enum):
76
84
OVAL= auto()
77
85
```
78
86
79
-
To disallow aliasing (_preventing duplicate values with different names_), the `@unique` decorator may be used.
87
+
To disallow aliasing (_preventing duplicate values with different names_), the [class decorator][class-decorator][`@enum.unique`][enum-unique-decorator] decorator may be used.
80
88
81
89
```python
82
-
@unique
90
+
@enum.unique
83
91
classShape(Enum):
84
92
CIRCLE=1
85
93
SQUARE=2
86
94
TRIANGLE=1
87
95
#=> ValueError: duplicate values found in <enum 'Shape'>: TRIANGLE -> CIRCLE
88
96
```
89
97
90
-
To access an enum member for a given value, this notation can be used: `EnumName(value)`.
98
+
To access an `enum` member for a given value, this notation can be used: `<EnumName>(<value>)`.
91
99
92
100
```python
93
101
g = Color(2)
@@ -99,15 +107,23 @@ g
99
107
#=> <Color.GREEN: 2>
100
108
```
101
109
102
-
A custom [restricted `Enum`][restricted-enums] can be written by subclassing `Enum` with any mix-in or data-type. For example:
110
+
A custom [restricted `Enum`][restricted-enums] can be written by subclassing the `Enum` class with any mix-in or data-type.
Copy file name to clipboardExpand all lines: concepts/enums/introduction.md
+14-6Lines changed: 14 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,27 @@
1
1
# Introduction
2
2
3
-
In Python, [an enum](https://docs.python.org/3/library/enum.html) is a set of names that are bound to unique `literal`, or `constant` values. Enums are defined by inheriting an `Enum` class. Built-in enum types are available in the module `enum` and the class `Enum` can be imported using `from enum import Enum`.
3
+
In Python, [an `enum`][enum-docs] is a set of names that are bound to unique `literal`, or `constant` values.
4
+
`Enums` are defined by inheriting from or subclassing an `Enum` class.
5
+
Built-in `enum` types are available in the module `enum` and the class `Enum` can be imported using `from enum import Enum`.
6
+
4
7
5
8
```python
9
+
from enum import Enum
10
+
6
11
classColor(Enum):
7
12
RED=1
8
13
GREEN=2
9
14
```
10
15
11
-
Note that the values of the enum members can be any data types such as str, tuple, float, etc.
16
+
Note that the values of the `enum` members can be any data types such as `str`, `tuple`, `float`, etc.
12
17
13
18
```python
14
19
classColor(Enum):
15
20
RED='red'
16
21
GREEN='green'
17
22
```
18
23
19
-
When assigning the same value to two members in an enum, the latter assigned member will be an alias to the formed one. It is not allowed to use the same name for two members of an enum.
24
+
When assigning the same value to two members in an `enum`, the latter assigned member will be an alias to the former one. It is not allowed to use the same name for two different members of an `enum`.
20
25
21
26
```python
22
27
classColor(Enum):
@@ -31,7 +36,7 @@ Color.ALIAS_OF_RED.value
31
36
#=> 1
32
37
```
33
38
34
-
Iterating through the members of the enum can be done with the standard `for member in` syntax:
39
+
Iterating through the members of the `enum` can be done with the standard `for member in` syntax:
35
40
36
41
```python
37
42
for member in Color:
@@ -40,7 +45,7 @@ for member in Color:
40
45
#=> (GREEN, 2)
41
46
```
42
47
43
-
Enum members can be compared using [`is` (_identity operator_)](https://www.w3schools.com/python/ref_keyword_is.asp) or `is not`. The `==` or `!=` (_equality_operators_) work likewise.
48
+
`Enum` members can be compared using [`is` (_identity operator_)][identity-keyword] or `is not`. The `==` or `!=` (_equality operators_) work likewise:
44
49
45
50
```python
46
51
a = Color.RED
@@ -52,7 +57,7 @@ a == Color.RED
52
57
#=> True
53
58
```
54
59
55
-
To access an enum member for a given value, `EnumName(value)` can be used:
60
+
To access an `enum` member for a given value, `<EnumName>(<value>)` can be used:
Copy file name to clipboardExpand all lines: concepts/fractions/about.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
# About
2
2
3
-
The [`Fractions`][fractions] module allows us to create and work with [`rational numbers`][rational]: fractions with an integer numerator divided by an integer denominator.
3
+
The [`fractions`][fractions] module allows us to create and work with [`rational numbers`][rational]: fractions with an integer numerator divided by an integer denominator.
4
4
5
5
For example, we can store `2/3` as an exact fraction instead of the approximate `float` value `0.6666...`
6
6
7
7
## Creating Fractions
8
8
9
9
10
10
Unlike `int`, `float`, and `complex` numbers, fractions do not have a literal form.
11
-
However, the fractions constructor is quite flexible.
11
+
However, the fraction constructor is quite flexible.
12
12
13
-
Most obviously, it can take take two integers.
14
-
Common factors are automatically removed, converting the fraction to its "lowest form": the smallest integers that accurately represent the fraction.
13
+
Most obviously, it can take two integers.
14
+
Common factors are automatically removed, converting the fraction to its "lowest form" (_the smallest integers that accurately represent the fraction_):
Copy file name to clipboardExpand all lines: concepts/fractions/introduction.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
# Introduction
2
2
3
-
The [`Fractions`][fractions] module allows us to create and work with [`rational numbers`][rational]: fractions with an integer numerator divided by an integer denominator.
3
+
The [`fractions`][fractions] module allows us to create and work with [`rational numbers`][rational]: fractions with an integer numerator divided by an integer denominator.
4
4
For example, we can store `2/3` as an exact fraction instead of the approximate `float` value `0.6666...`.
5
5
6
6
Unlike `int`, `float`, and `complex` numbers, fractions do not have a literal form.
7
-
However, the fractions constructor is quite flexible.
7
+
However, the fraction constructor is quite flexible.
8
8
9
-
Most obviously, it can take take two integers as arguments.
10
-
Common factors are automatically removed, converting the fraction to its "lowest form": the smallest integers that accurately represent the fraction:
9
+
Most obviously, it can take two integers as arguments.
10
+
Common factors are automatically removed, converting the fraction to its "lowest form" (_the smallest integers that accurately represent the fraction_):
0 commit comments