Enumerations are not new to Java, so this article explains why you should use them in Python.

Imagine that you are working on a project that has JSON data requested through a Rest API and has a data format like this:

{
  metrics: 'speed',
  value: 20,
  weekday: 'Mon'
}
Copy the code

You need to process data, converting a string day of the week into a numeric number of 1, 2, 3, 4, 5, 6, 7. How do you do that?

Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
Sun = 7
Copy the code

Predefine a one-to-one mapping in the manner of defining constants.

If you have some experience in Python development, most of you will choose dictionary form to solve the problem:

weekday = {
  'Mon': 1
  'Tue': 2
  'Wed': 3
  'Thu': 4
  'Fri': 5
  'Sat': 6
  'Sun': 7
}
Copy the code

The corresponding numeric value is obtained by means of a key value.

However, as I mentioned in another article (from Bronze to King, save 90%), dictionaries are a very memory intensive way to store data in Python.

If you read my article, you might come up with a lot of new ideas, such as implementing enumeration relationships in a class way,

class Weekday:
  Mon = 1
  Tue = 2
  Wed = 3
  Thu = 4
  Fri = 5
  Sat = 6
  Sun = 7
Copy the code

However, these approaches are not ideal solutions for data persistence and extensibility.

If you have a certain amount of Java development experience, faced with such a problem, you will immediately come to mind the concept of enumerated classes.

Enum Weekday {MON (1, "Monday"), TUE (2, "Tuesday"), and WED (3, "on Wednesday"), THU (4, "Thursday"), FRI (5, "Friday"), the SAT (6, "Saturday"), and the SUN (7, "seven weeks"); publicfinalint intValue; publicfinal String chinese; Weekday(int intValue, String chinese) { this.intValue = intValue; this.chinese = chinese; }}Copy the code

You can access values in an enumerated class by,

publicclass App { public static void main(String[] args) { Weekday day = Weekday.MON; System.out.println(day.intValue); System.out.println(day.chinese); }}Copy the code

Enumerated classes in Java have many advantages, such as security and specification, that make them popular.

So, can you use enumeration classes to solve similar enumeration problems in Python?

The answer is yes.

Python provides a standard enum library that you can implement Python enumerated classes by calling and inheriting from.

There are two ways to do this.

Method 1: Class inheritance

# demo.py

from enum import Enum

class Weekday(Enum):
  Mon = 1
  Tue = 2
  Wed = 3
  Thu = 4
  Fri = 5
  Sat = 6
  Sun = 7
  
print(Weekday)
print(Weekday.Mon)
print(Weekday.Mon.value)
Copy the code

So if YOU look at the output,

$ python demo.py
<enum 'Weekday'>
Weekday.Mon
1
Copy the code

Method 2: Define variables

When the value of each enumeration is incremental and unimportant, you can call the Enum class directly with the constant name as a string,

Weekday = Weekday('Weekday', ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
for name, member in Weekday.__members__.items():
	print(name, member, member.value)
Copy the code

Now, if I look at the output,

$ python demo.py ('Mon', <Weekday.Mon: 1>, 1) ('Tue', <Weekday.Tue: 2>, 2) ('Wed', <Weekday.Wed: 3>, 3) ('Thu', <Weekday.Thu: 4>, 4) ('Fri', <Weekday.Fri: 5>, 5) ('Sat', <Weekday.Sat: 6>, 6) ('Sun', <Weekday.Sun: > 7, 7)Copy the code

We can also access the value of the corresponding date by,

sunday = Weekday.Sun.value
Copy the code

This concludes the use of enumerated classes.

Python is a relatively simple programming language. The so-called simplicity here is simple to get started. If you really want to become a high-level Python developer, there is a lot of knowledge that is not available in books.

Like the enumerated classes in this article, perhaps you can solve this problem by defining constants and using dictionaries. However, when it comes to enumeration problems, using these entry-level approaches can compromise the efficiency and readability of your code. Therefore, when confronted with a requirement, implementing functionality should not be the end goal, but implementing a high-performance system should be the end goal.

I remember reading an interview with Lei Jun in which he mentioned that he wrote code like poetry, although there was a lot of fun on the Internet for his comment. However, I think this is something that programmers should aspire to, and most programmers don’t.


Dry recommended

In order to facilitate everyone, I spent half a month’s time to get over the years to collect all kinds of iso technical finishing together, content including but not limited to, Python, machine learning, deep learning, computer vision, recommendation system, Linux, engineering, Java, content of up to 5 t +, I put all the resources download link to a document, The directory is as follows:

All dry goods to everyone, hope to be able to support it!

https://http://pan.baidu.com/s/1eks7CUyjbWQ3A7O9cmYljA (code: 0000)