Python中的枚举中的抽象方法

发布于 2025-02-04 07:57:53 字数 1299 浏览 2 评论 0原文

我的问题是,如何在Python中写这篇文章?这样的事情吗?

它应该如何工作: 我正在从算法中获取数据,该算法将决定输出哪个字母。如果数据中不应适用于一个字符的某些条件,则应检查其他字符的条件。数据和条件当然比此处显示的要复杂。

为什么要枚举: 因为只有这种小主要方法必须写在算法文件中(具有算法)。并将字母的条件封装在另一个文件中并明确结构。

enum Letter {
    
    A () {
        public boolean condition(int[] args) {
            if (args[0] > args[1]) return false;
            if (args[1] > args[2]) return false;
            return true;
        }
    },
    
    B () {
        public boolean condition(int[] args) {
            if (args[0] > args[1]) return false;
            if (args[1] < args[2]) return false;
            return true;
        }
    },
    
    C () {
        public boolean condition(int[] args) {
            if (args[0] < args[1]) return false;
            if (args[1] < args[2]) return false;
            return true;
        }
    };
    
    public abstract boolean condition(int[] args);
    
}
public class Alphabet {
    
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        //int[] arr = {1, 2, 1};
        //int[] arr = {3, 2, 1};
        
        for (Letter l : Letter.values()) {
            if (l.condition(arr)) {
                System.out.println(l);
                break;
            }
        }
    }
}

My question is, how to write this in python? Is something like this possible?

How it should work:
I'm getting data from an algorithm that will decide which letter to output. If there are certain conditions in the data that should not apply to one character, the conditions should be checked for another character. The data and the conditions are of course more complex than shown here.

Why enums:
Because only this small main method has to be written in the algorithm file (iterable). And the conditions of the letters are encapsulated in another file and clearly structured.

enum Letter {
    
    A () {
        public boolean condition(int[] args) {
            if (args[0] > args[1]) return false;
            if (args[1] > args[2]) return false;
            return true;
        }
    },
    
    B () {
        public boolean condition(int[] args) {
            if (args[0] > args[1]) return false;
            if (args[1] < args[2]) return false;
            return true;
        }
    },
    
    C () {
        public boolean condition(int[] args) {
            if (args[0] < args[1]) return false;
            if (args[1] < args[2]) return false;
            return true;
        }
    };
    
    public abstract boolean condition(int[] args);
    
}
public class Alphabet {
    
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        //int[] arr = {1, 2, 1};
        //int[] arr = {3, 2, 1};
        
        for (Letter l : Letter.values()) {
            if (l.condition(arr)) {
                System.out.println(l);
                break;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2025-02-11 07:57:53

While you could shoehorn an Enum into serving this purpose, it'd be much easier to just use a dict or list of functions that you can iterate over, eg:

letters = [
    ("A", lambda args: args[0] <= args[1] <= args[2]),
    ("B", lambda args: args[0] <= args[1] >= args[2]),
    ("C", lambda args: args[0] >= args[1] >= args[2]),
]

def alphabet(args):
    print(next(letter for letter, condition in letters if condition(args)))

alphabet([1, 2, 3])  # A
alphabet([1, 2, 1])  # B
alphabet([3, 2, 1])  # C

Or with type annotations (these are optional in Python, but they allow you to do static type analysis/testing similar to what you get in a compiled language, and can make tricky code easier to understand):

from typing import Callable, List, Tuple

Condition = Callable[[List[int]], bool]

letters: List[Tuple[str, Condition]] = [
    ("A", lambda args: args[0] <= args[1] <= args[2]),
    ("B", lambda args: args[0] <= args[1] >= args[2]),
    ("C", lambda args: args[0] >= args[1] >= args[2]),
]

def alphabet(args: List[int]) -> None:
    print(next(letter for letter, condition in letters if condition(args)))

Note that in the type-annotated version, we can just define Condition作为“获取INT列表并返回bool的函数”的方便类型别名。我们实际上不需要实际组建一个新类或定义新的抽象接口,因为功能是Python中的一流对象,它们的接口已经完全拟合了此用例。

While you could shoehorn an Enum into serving this purpose, it'd be much easier to just use a dict or list of functions that you can iterate over, e.g.:

letters = [
    ("A", lambda args: args[0] <= args[1] <= args[2]),
    ("B", lambda args: args[0] <= args[1] >= args[2]),
    ("C", lambda args: args[0] >= args[1] >= args[2]),
]

def alphabet(args):
    print(next(letter for letter, condition in letters if condition(args)))

alphabet([1, 2, 3])  # A
alphabet([1, 2, 1])  # B
alphabet([3, 2, 1])  # C

Or with type annotations (these are optional in Python, but they allow you to do static type analysis/testing similar to what you get in a compiled language, and can make tricky code easier to understand):

from typing import Callable, List, Tuple

Condition = Callable[[List[int]], bool]

letters: List[Tuple[str, Condition]] = [
    ("A", lambda args: args[0] <= args[1] <= args[2]),
    ("B", lambda args: args[0] <= args[1] >= args[2]),
    ("C", lambda args: args[0] >= args[1] >= args[2]),
]

def alphabet(args: List[int]) -> None:
    print(next(letter for letter, condition in letters if condition(args)))

Note that in the type-annotated version, we can just define Condition as a handy type alias for "a function that takes a list of ints and returns a bool". We don't need to actually make a new class or define a new abstract interface, since functions are first-class objects in Python and their interface already fits this use case perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文