Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum not iterable in Python 2.7

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.

like image 420
shr Avatar asked Apr 21 '15 10:04

shr


People also ask

Is enum iterable Python?

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.

Is enum an iterable?

Enumeration is like an Iterator , not an Iterable . A Collection is Iterable . An Iterator is not.

Is enum ordered Python?

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.

How do you print an enum in Python?

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.


1 Answers

The backport of the Python 3.4 Enum type is enum34, not enum.

like image 158
Ethan Furman Avatar answered Sep 27 '22 16:09

Ethan Furman