from enum import Enum
class Shake(Enum):
__order__ = 'vanilla chocolate cookies mint' # only needed in 2.x
vanilla = 7
chocolate = 4
cookies = 9
mint = 3
for shake in Shake:
print shake
Getting error while running this code
for shake in Shake:
TypeError: 'type' object is not iterable
Is iteration not supported for Enum
in Python 2.7? It works if we make a object of the Enum
type.
Printing enum as an iterableWe can print the enum as an iterable list. In the below code we use a for loop to print all enum members.
Enumeration is like an Iterator , not an Iterable . A Collection is Iterable . An Iterator is not.
Because Python's enum. Enum does not provide ordering by default, ostensibly to eliminate C-style (mis-)treatment of enumerations as thin wrappers over integral types.
The __str__() method is called by str(object) and the built-in functions format() and print() and returns the informal string representation of the object. Now you can get the value of the enum directly, without accessing the value attribute on the enum member. You can also use square brackets to access enum members.
The backport of the Python 3.4 Enum type is enum34, not enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With