Python 中检查对象类型的字符串与枚举
假设我有一个名为 Tag 的对象,并且我有由实例变量通过以下方式指示的三种类型的标签,
class Tag(object):
def __init__(self, name, type):
self.name = name
self.type = type
t1 = Tag("blue", "cold")
t2 = Tag("red", "warm")
t3 = Tag("black", "hot")
假设我只允许三种类型:冷、暖和热。最好去检查一下它是否是这样的类型之一?
if t1.type == "cold":
# do something
elif t1.type == "warm":
# do something else
else t1.type == "hot":
# do something even elser
或者我应该创建一个类似枚举的对象,例如 问题,
class Type:
COLD=1
WARM=2
HOT=3
而是创建这样的标签?
t1 = Tag("blue", Type.COLD)
我问这个问题的原因是因为我听说比较字符串需要大量的处理能力,即使这些是 3、4 个字母长的单词,我也可能会对这些类型进行数万次比较。您认为像上面所示的示例中那样创建枚举对象来确定对象的类型值得吗?或者有更好的方法来做我想做的事情吗?
Say I have an object called Tag, and I have three types of tags as indicated by an instance variable in the following way,
class Tag(object):
def __init__(self, name, type):
self.name = name
self.type = type
t1 = Tag("blue", "cold")
t2 = Tag("red", "warm")
t3 = Tag("black", "hot")
Let's say I only allowed three types: cold, warm, and hot. Would it be better to go checking if it is one of these types like this?
if t1.type == "cold":
# do something
elif t1.type == "warm":
# do something else
else t1.type == "hot":
# do something even elser
Or should I create an enum-like object like the one from this question,
class Type:
COLD=1
WARM=2
HOT=3
And instead create Tags like this?
t1 = Tag("blue", Type.COLD)
The reason I ask this question is because I heard a lot of processing power goes into comparing strings, and even though these are short 3, 4 letter long words, it is possible that I'd be making tens of thousands of comparisons of these types. Do you think its worth it to go creating enum objects for determining the type of an object as in the example I've shown above? Or is there a better way to do what I'm attempting to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
性能差异可能不足以让您担心。如果您担心性能,您应该进行一个简单的测试。使用Python的timeit模块来测试这两种情况的性能。
It's possible the performance difference may not be significant enough for you to worry about it. You should make a simple test if you're concerned about performance. Use Python's timeit module to test the performance of both cases.
我不会担心它的性能,除非分析表明它确实是一个问题。如果您使用 IDE,则枚举方法具有拼写错误检查的优点。
I wouldn't be worried about the performance of this unless profiling shows that it indeed is a problem. If you're using an IDE, the enum approach has the benefit of typo-checking.
我会采用组合方法 - 让“枚举”成为 Tag 类的一部分,接受字符串进行初始化,然后将其转换。此外,您可以使用字典来创建调度表,而不是使用一堆 if/else 分支。
I would go with a combination approach -- have the 'enum' be part of the Tag class, accept a string for initialization, and then convert it. Also, instead of using a bunch of if/else branches, you can use a dict to create a dispatch table.
在 python 中,使用字典进行调度通常比 if/elif/else 塔更有利。
所以:
少量的额外代码可以自动构建调度表:
In python, it's frequently advantageous to use a dictionary for dispatch rather than a tower of if/elif/else.
So:
And a small additional bit of code can build the dispatch table automatically: