How to define globals?

This question already has an answer here: How can I represent an 'Enum' in Python? 43 answers How do I create a constant in Python? 29 answers In Python there are no true constants. The closest thing you can do is create a variable and never reassign it. Your attempt looks like it will work fine. One thing to note is that "constant and true" does not mean "globa

如何定义全局变量?

这个问题在这里已经有了答案: 我如何在Python中表示'Enum'? 43个答案 如何在Python中创建常量? 29个答案 在Python中没有真正的常量。 您可以做的最接近的事情是创建一个变量,并且不会重新分配它。 你的尝试看起来会很好。 有一点要注意的是,“不变而真实”并不意味着“全球”。 全局变量的区别特征与常量无关,而是可以引用对象的范围(即全部)。 您可以拥有不常量的全局变量(当然,在允许常量的语言中

Is there any way to define the enum in python?

This question already has an answer here: How can I represent an 'Enum' in Python? 43 answers OFFLINE = 1 ONLINE = 2 LOGIN = 3 You can wrap it in a class if you want STATUS_T.OFFLINE style of access. Python is a dynamically typed language so the concept of an enum makes little sense, all you can do is have range of meaningful values you can set something to. Apparently, an equival

有没有什么办法来定义在Python中的枚举?

这个问题在这里已经有了答案: 我如何在Python中表示'Enum'? 43个答案 OFFLINE = 1 ONLINE = 2 LOGIN = 3 如果您希望获得STATUS_T.OFFLINE样式的访问权限,则可以将其封装在类中。 Python是一种动态类型语言,因此枚举的概念毫无意义,你所能做的就是拥有一些你可以设置的有意义的值。 显然,在3.4中添加了一个等价物,请参阅如何在Python中表示'Enum'? 不是真的。 最常见的习惯用法是在课堂上定

C++ enum instance equivalent in Python

This question already has an answer here: How can I represent an 'Enum' in Python? 43 answers Here's what i found: How can I represent an 'Enum' in Python? tl;dr: class Animal: DOG = 1 CAT = 2 x = Animal.DOG Until Python 3.4, the Pythonic approach would be to just use integers: MODE_UPPERCASE, MODE_LOWERCASE, MODE_PUNCTUATION = range(3) mode = MODE_UPPERCASE swi

Python中的C ++枚举实例

这个问题在这里已经有了答案: 我如何在Python中表示'Enum'? 43个答案 这是我发现的: 我如何在Python中表示'Enum'? TL;博士: class Animal: DOG = 1 CAT = 2 x = Animal.DOG 在Python 3.4之前,Pythonic的方法就是使用整数: MODE_UPPERCASE, MODE_LOWERCASE, MODE_PUNCTUATION = range(3) mode = MODE_UPPERCASE switch_mapping = { MODE_UPPERCASE: handle_upper, # etc. } 如果

using function attributes as return values

This question already has an answer here: How can I represent an 'Enum' in Python? 43 answers While you can, I wouldn't, especially now that Python has an Enum type in 3.4 which has been backported. Your example above might look like this as an Enum : class Size(Enum): small = 1 medium = 2 large = 3 @classmethod def classify(cls, number): if numbe

使用函数属性作为返回值

这个问题在这里已经有了答案: 我如何在Python中表示'Enum'? 43个答案 虽然你可以,但我不会,特别是现在Python有一个3.4的Enum类型,它已被回溯。 上面的例子可能看起来像一个Enum : class Size(Enum): small = 1 medium = 2 large = 3 @classmethod def classify(cls, number): if number < 10: return cls.small elif number < 20: re

Enum class in python

This question already has an answer here: How can I represent an 'Enum' in Python? 43 answers I would recommend to achieve you goal using metaclasses, in order to minimise the client code. So first of all checkout the below metaclass: class EnumMeta(type): def __getattribute__(self, name): actual_value = super(EnumMeta, self).__getattribute__(name) if isinstance

Python中的枚举类

这个问题在这里已经有了答案: 我如何在Python中表示'Enum'? 43个答案 我会建议使用元类实现您的目标,以最大限度地减少客户端代码。 所以首先检查下面的元类: class EnumMeta(type): def __getattribute__(self, name): actual_value = super(EnumMeta, self).__getattribute__(name) if isinstance(actual_value, self): return actual_value else: new_

What's the cleanest way to set up an enumeration in Python?

This question already has an answer here: How can I represent an 'Enum' in Python? 43 answers Enums were added in python 3.4 (docs). See PEP 0435 for details. If you are on python 2.x, there exists a backport on pypi. pip install enum34 Your usage example is most similar to the python enum's functional API: >>> from enum import Enum >>> MyEnum = Enum('MyEn

在Python中设置枚举最简洁的方法是什么?

这个问题在这里已经有了答案: 我如何在Python中表示'Enum'? 43个答案 在python 3.4(docs)中添加了Enums。 详情请参阅PEP 0435。 如果您使用的是Python 2.x,则在pypi上存在一个backport。 pip install enum34 您的使用示例与python enum的功能性API非常相似: >>> from enum import Enum >>> MyEnum = Enum('MyEnum', 'APPLE BANANA WALRUS') >>> MyEnum.BANANA <MyEnum.BA

pythonic replacement for enums

In my python script i am parsing a user created file and typically there will be some errors and there are cases were i warn the user to be more clear. In ci would have an enum like eAssignBad, eAssignMismatch, eAssignmentSignMix (sign mixed with unsigned). Then i would look the value up to print an error or warning msg. I link having the warningMsg in one place and i like the readability of n

用于枚举的pythonic替换

在我的python脚本中,我正在解析用户创建的文件,通常会出现一些错误,并且有些情况下我警告用户要更清楚。 在ci中会有一个枚举类似eAssignBad,eAssignMismatch,eAssignmentSignMix(与无符号混合的符号)。 然后,我会查看打印错误或警告消息的值。 我将warningMsg链接到一个地方,我喜欢名称的可读性而不是字面值。 这是什么pythonic替代? 重复 :我如何在Python中表示'Enum'? 这是迄今为止我发现的最好的

Python's enum equivalent

Possible Duplicate: What's the best way to implement an 'enum' in Python? What is the Python idiom for a list of differently indexed names (like Enum in C/C++ or Java)? Clarification: I want a property of a value to be set to a restricted range, such as card suites Heart, Club, Spade, Diamond . I could represent it with an int in range 0..3, but it would allow out of range inpu

Python的enum等价物

可能重复: 什么是在Python中实现'枚举'的最佳方式? 什么是不同索引名称列表的Python成语(如C / C ++或Java中的Enum)? 澄清:我希望将一个值的属性设置为受限制的范围,例如Card Suite Heart, Club, Spade, Diamond 。 我可以用范围为0..3的int来表示它,但它可以允许超出范围输入(如15)。 class Suite(object): pass class Heart(Suite): pass class Club(Suite): pass 等等 Python中的类是一个对象

Enumerations in python

Duplicate: What's the best way to implement an 'enum' in Python? Whats the recognised way of doing enumerations in python? For example, at the moment I'm writing a game and want to be able to move "up", "down", "left" and "right". I'm using strings because I haven't yet figured out how enumerations work in python, and so my lo

枚举在python中

重复: 什么是在Python中实现'枚举'的最佳方式? 什么是在python中进行枚举的公认方式? 例如,现在我正在写一个游戏,并希望能够“向上”,“向下”,“向左”和“向右”移动。 我使用的是字符串,因为我还没有弄清楚枚举在python中是如何工作的,所以我的逻辑充斥着这样的东西: def move(self, direction): if direction == "up": # Do something 我想用诸如Directions.up东西替换"up" class

What's the common practice for enums in Python?

Possible Duplicate: How can I represent an 'enum' in Python? What's the common practice for enums in Python? Ie how are they replicated in Python? public enum Materials { Shaded, Shiny, Transparent, Matte } class Materials: Shaded, Shiny, Transparent, Matte = range(4) >>> print Materials.Matte 3 I've seen this pattern several times: >>&g

Python中枚举的常用做法是什么?

可能重复: 我如何在Python中表示'枚举'? Python中枚举的常用做法是什么? 即他们如何在Python中复制? public enum Materials { Shaded, Shiny, Transparent, Matte } class Materials: Shaded, Shiny, Transparent, Matte = range(4) >>> print Materials.Matte 3 我已经多次看到这种模式: >>> class Enumeration(object): def __init__(self, names): # or