preface

The blogger thought twice and offered a course on the basics of data structures. Because most of the current data structure books are using C/C++, which undoubtedly increases the threshold of learning.

However, Python language is relatively easy to get started and master. Learning data structures and algorithms in Python seems more friendly to beginners.

In this article, we first introduce enumeration types. In the Python library, the enum module is provided. With this module, we can define literal integers and strings that programmers can easily understand.

Create an enumeration

Without further comment, let’s create a simple enumeration class: week. That is, an enumeration class that makes it easy for programmers to understand the day of the week. The specific definition is as follows:

import enum

class Week(enum.Enum) :
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6
    Sunday = 7

print("name", Week.Monday.name)
print("value", Week.Monday.value)
Copy the code

After running, the console output looks like this:

From the above code, we know that to define an enumeration class, enums must be derived. You also define the class attribute name that describes the value and the concrete value value.

Iterative enumeration

Here, because we are actively defining the enumerated class, we know the names and values. But most of the time, we refer to an enumerated class defined by someone else, and we don’t know exactly what name and value are. At this point, you need to traverse the history to get the members of its enumeration.

Next, let’s go through the week enumeration above. The code looks like this:

import enum


class Week(enum.Enum) :
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6
    Sunday = 7


for i in Week:
    print("name=", i.name, end="")
    print("value=", i.value)
Copy the code

After running, the name and value are displayed.

Compare the enumeration

The enumeration value can be of any type. So enumerated types don’t compare size, only equality. Let’s look at a simple example:

import enum


class Week(enum.Enum) :
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6
    Sunday = 7


one_Week = Week.Monday
print(one_Week == Week.Monday)
print(one_Week == Week.Tuesday)
print(one_Week > Week.Tuesday)
Copy the code

After running, the effect is as follows:

As you can see, TypeError is generated when comparing enumeration sizes.

enum.IntEnum

Of course, if you want to compare the size of enumerations. You can also change the inherited enumeration type. For example, if the week enumeration above is an integer, it is directly derived from the enumerated integer type. In this way, you can compare with each other, the specific code is as follows:

import enum


class Week(enum.IntEnum) :
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6
    Sunday = 7


one_Week = Week.Monday
print(one_Week == Week.Monday)
print(one_Week == Week.Tuesday)
print(one_Week > Week.Tuesday)
Copy the code

After running, the effect is as follows:

Unique enumeration value

After learning this, has any reader tried to define two different names equal the same value in the same enumeration? In this case, how do you handle enumerations?

Let’s look at some code first:

import enum


class Week(enum.IntEnum) :
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6
    Sunday = 7

    Mon= 1
    Tue= 2


for i in Week:
    print("name=", i.name, end="")
    print("value=", i.value)
Copy the code

Here, the blogger added 2 weeks to the week enumeration, which is also equal to 1,2. After running, you will find that there is no output for these two values. This is because, by default, an enumeration member with the same value is considered an alias of the previous member. This has the advantage of preventing duplicate values during iteration.

If you want to define an enumeration type that has no members of the same value. You can add the modifier @unique to the enumerated class you define. At this point, if there are members with the same value, an error will be reported.

import enum

@enum.unique
class Week(enum.IntEnum) :
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6
    Sunday = 7
    
    Mon= 1
    Tue= 2
Copy the code

Create an enumeration in the code

In practice, we often do not define enumerated classes directly. Instead, enumerations are generated dynamically when the program needs them. At this point, there are two ways to define:

  1. Automatically generate values for enumeration types:
import enum

Week=enum.Enum(
    value="Week",
    names=('Monday Tuesday Wednesday Thursday Friday Saturday Sunday '),for i in Week:
    print("name=", i.name, end="")
    print("value=", i.value)
Copy the code

Here, each member variable in name is automatically assigned from 1, and the member variables in names are separated by Spaces. It works the same as above, not shown here.

  1. Manually generate values for the enum type:
import enum

Week = enum.Enum(
    value="Week",
    names=[
        ('Monday'.1),
        ('Tuesday'.2),
        ('Wednesday'.3),
        ('Thursday'.4),
        ('Friday'.5),
        ('Saturday'.6),
        ('Sunday'.7),,)for i in Week:
    print("name=", i.name, end="")
    print("value=", i.value)
Copy the code

Created this way, you can manually assign a value to each member. To say which is more convenient, or to combine the actual situation to judge.