Hướng dẫn nested enum python

it may be a bit late and the one who asked the question is no longer necessary, but I leave it here in case someone wants to take a look at it, and even if it has already been validated as one, although the same comment that it is not completely complete .

But I have been thinking about it and in the end I have solved it by looking at the same documentation XD.

You cannot extend classes of Enums, but you can extend methods, I have followed this way and the only thing I have done has been to override the new and init methods, the use case can be modified, this is only to nest enumerators.

from enum import Enum

class SuperNestedEnum(Enum):
    def __new__(cls, *args):
        obj = object.__new__(cls)
        value = None
        # Normal Enumerator definition
        if len(args) == 1:
            value = args[0]

        # Have a tuple of values, first de value and next the nested enum (I will set in __init__ method)
        if len(args) == 2:
            value = args[0]

        if value:
            obj._value_ = value

        return obj

    def __init__(self, name, nested=None):
        # At this point you can set any attribute what you want
        if nested:
            # Check if is an Enumerator you can comment this if. if you want another object
            if isinstance(nested, EnumMeta):
                for enm in nested:
                    self.__setattr__(enm.name, enm)


class Homework(Enum):
    Task = "5"

class Subjects(SuperNestedEnum):
    Maths = "maths"
    English = "english"
    Physics = "nested", Homework

class School(SuperNestedEnum):
    Name = "2"
    Subjects = "subjects", Subjects

Ignore the use case because it doesn't make sense, it's just an example

>>> School.Name


>>> School.Subjects


>>> School.Subjects.value
'subjects'

>>> School.Subjects.Maths


>>> School.Subjects.Physics.value
'nested'

>>> School.Subjects.Physics.Task


>>> School.Subjects.Physics.Task.value
'5'

Hướng dẫn nested enum python

Cú pháp câu lệnh for trong python

Vòng lặp for trong python được sử dụng để lặp một biến qua một dãy (List hoặc String) theo thứ tự mà chúng xuất hiện. Vòng lặp for trong python được sử ...

Hướng dẫn nested enum python

How do you print a whole dictionary in python?

In this article, we will discuss different ways to print line by line the contents of a dictionary or a nested dictionary in python.As dictionary contains items as key-value pairs. So, first, ...

Hướng dẫn nested enum python

Is named even in python?

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)Python Basic: Exercise-21 with SolutionWrite a Python program to find whether a given number (accept from the user) is even or odd, print out ...

Hướng dẫn nested enum python

How do you check a vowel in python?

In this post, we will write a Python program to check whether the entered character is vowel or consonant.Python CodeIn this program, user is asked to input a character. The program checks whether ...

Hướng dẫn nested enum python

Hướng dẫn read file yaml python

YAML là gì?YAML (YAML Ain’t Markup Language) là một chuẩn dữ liệu kiểu serialization dành cho tất cả các ngôn ngữ. Nó được sử dụng phổ biến để tạo ra các ...

Hướng dẫn nested enum python

Python sql escape special characters

Welcome to the world of string encoding formats!tl;dr - The preferred method for handling quotes and escape characters when storing data in MySQL columns is to use parameterized queries and let the ...

Hướng dẫn nested enum python

How to store string in python

When Python wants to store text in a variable, it creates a variable called a string. Katie Cunningham shows you how to create, print and format strings and when to use strings in the real world. ...

Hướng dẫn nested enum python

Is numpy already installed with python?

The numpy Python module is widely used for many different analyses and as a dependency for many other Python packages. In many instances, it is necessary to determine if numpy is installed and which ...

Hướng dẫn nested enum python

Calculate conditional probability in python

Use python to calculate the conditional probability of a student getting an A in math given they missed 10 or more classes.This article has 2 parts:1. Theory behind conditional probability2. Example ...

Hướng dẫn nested enum python

What is kth in python?

Given a List, extract Kth occurrence of Even Element.Input : test_list = [4, 6, 2, 3, 8, 9, 10, 11], K = 3Output : 8Explanation : K = 3, i.e 0 based index, 4, 6, 2 and 4th is 8.Input : test_list = ...

Hướng dẫn nested enum python

Hướng dẫn data structure in python

Nội dung chínhNode: An individual part of a larger data structureOrphaned nodesNull node linkPython Node implementationLearn More on Codecademy1. Linked List là cái gì?Linked List là tập ...

Hướng dẫn nested enum python

Hướng dẫn tách câu trong python

Hướng dẫn cách tách chuỗi trong python. Bạn sẽ học được cách tách chuỗi trong python bằng các phương thức có sẵn như split và splitlines trong bài viết ...

Hướng dẫn nested enum python

Hướng dẫn nested enum python

Hướng dẫn nested enum python

Hướng dẫn reactjs frontend python backend

Im not quite sure if my question is a duplicate, but I wasnt able to find something helping me in my case. Nội dung chínhBeginner React tutorialsPython+React tutorialsOther React resourcesDo you ...

Hướng dẫn nested enum python

How do you repeat a run in python?

A good way of organizing your code is to put your main program into a function called main() or similar:def main(): sentence = input(Please enter sentence(s): ) num_words = ...

Hướng dẫn nested enum python

How do you split a string delimited in python?

❮ String MethodsExampleSplit a string into a list where each word is a list item: txt = welcome to the junglex = txt.split() print(x)Try it Yourself »Definition and UsageThe split() method ...

Hướng dẫn nested enum python

Hướng dẫn dùng set -x python

Dẫn nhậpTrong các bài trước, Kteam đã giới thiệu đến bạn một số container của Python.Nội dung chínhDẫn nhậpGiới thiệu về Set trong PythonCách khởi tạo ...

Hướng dẫn nested enum python

How do you add options in python?

I have a Python script a.py and b.py, Id like to call the main function of b.py and pass arguments from a.py. How to add option in a.py from inside the code and pass it to the main of b.py ?. I ...

Hướng dẫn nested enum python

Hướng dẫn nested enum python

What is a dictionary in python?

thisdict = { brand: Ford, model: Mustang, year: 1964 } DictionaryDictionaries are used to store data values in key:value pairs.A dictionary is a ...

Hướng dẫn nested enum python

Hướng dẫn python del keyerror

Im trying to iterate through a Python dictionary using a for loop and then delete one of the keys/value pairs but Im getting a KeyError: Not Found.Here is my Code.cars = { brand: Tesla, ...

Hướng dẫn nested enum python

Hướng dẫn nested enum python

How to reduce if else statement in python

All you need to do in this case is construct a string from the integer value self.timeselect.get().selectedchoice = self.timeselect.get() if 0 < selectedchoice < 73: orig_stdout = ...

Hướng dẫn nested enum python

Which python libraries support threads?

Source code: Lib/threading.pyThis module constructs higher-level threading interfaces on top of the lower level _thread module.Changed in version 3.7: This module used to be optional, it is now ...

Hướng dẫn nested enum python

Hướng dẫn nested enum python

Hướng dẫn nested enum python

How do you write a triangle program in python?

Programs to print triangles using *, numbers and charactersExample 1: Program to print half pyramid using ** * * * * * * * * * * * * * *Source Coderows = int(input(Enter number of rows: )) for i ...

Hướng dẫn nested enum python

How to create a sheet in excel using python

I have browsed almost all the previous threads, but still cannot get this working.I am trying to add a new sheet to an existing workbook. My code works,but it keeps on adding a lot more (so many of ...

Hướng dẫn nested enum python

Hướng dẫn python text similarity

In this article, you will learn about different similarity metrics and text embedding techniques. By the end, youll have a good grasp of when to use what metrics and embedding techniques. You’ll ...

Hướng dẫn nested enum python

Hướng dẫn app lập trình python

Python được Guido van Rossum tạo ra năm 1991, và hiện nay Python là một trong những ngôn ngữ lập trình phổ biến nhất dành cho người mới bắt đầu, các chuyên gia ...

Hướng dẫn nested enum python

Hướng dẫn python programming patterns

Nội dung chính Nội dung chính Các hàm Regex Xây dựng biểu thức chính quy Meta-Characters Ký tự đặc biệt Hàm findall() Đối tượng Match (kết quả khớp) Các ...

Hướng dẫn nested enum python

Hướng dẫn nested enum python

Handling io exception in python

PythonServer Side ProgrammingProgramming Beyond Basic Programming - Intermediate PythonMost Popular 36 Lectures 3 hours Mohammad Nauman More Detail Practical Machine Learning using PythonBest ...

Hướng dẫn nested enum python

What is meant by raw string in python?

Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash () as a literal character. This is useful when we want to have a string that ...

Hướng dẫn nested enum python

How do i use the randint module in python?

IntroductionIn this tutorial, we are going to focus on the randint() method in Python. In our previous tutorials, we saw different random number generating methods defined inside the random module in ...

Hướng dẫn nested enum python

How to work with xml in python

XML is a portable, open source language that allows programmers to develop applications that can be read by other applications, regardless of operating system and/or developmental language.What is ...

Hướng dẫn nested enum python

Split with or condition python

You can replace the : with something (just make sure that this is something that doesn`t exist in the string in other place... you can use a long term or something), and than split by : and ...

Hướng dẫn nested enum python

What are python built in functions?

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. Built-in ...

Hướng dẫn nested enum python

Hướng dẫn python dsa

A byte of python tiếng việtLập trình Python 203 ViewsA byte of Python là quyển sách lập trình Python hoàn toàn miễn phí. Được xem như bài hướng dẫn cho những người ...