How to implement a subscriptable class in Python (subscriptable class, not subscriptable object)?

Question:

To implement a subscriptable object is easy, just implement __getitem__ in this object’s class definition.
But now I want to implement a subscriptable class. For example, I want to implement this code:

class Fruit(object):
    Apple = 0
    Pear = 1
    Banana = 2
    #________________________________ 
    #/ Some other definitions,         
    # make class 'Fruit' subscriptable. /
    # -------------------------------- 
    #           ^__^
    #           (oo)_______
    #            (__)       )/
    #                ||----w |
    #                ||     ||

print Fruit['Apple'], Fruit['Banana']
#Output: 0 2

I know getattr can do the same thing, but I feel subscript accessing is more elegant.

Asked By: user805627

||

Answers:

Seems to work by changing the metaclass. For Python 2:

class GetAttr(type):
    def __getitem__(cls, x):
        return getattr(cls, x)

class Fruit(object):
    __metaclass__ = GetAttr

    Apple = 0
    Pear = 1
    Banana = 2

print Fruit['Apple'], Fruit['Banana']
# output: 0 2

On Python 3, you should use Enum directly:

import enum

class Fruit(enum.Enum):
    Apple = 0
    Pear = 1
    Banana = 2

print(Fruit['Apple'], Fruit['Banana'])
# Output: Fruit.Apple, Fruit.Banana
print(Fruit['Apple'].value, Fruit['Banana'].value)
# Output: 0 2
Answered By: kennytm

Add something like this to your class:

class Fruit(object):
     def __init__(self):
         self.Fruits = {"Apple": 0, "Pear": 1, "Banana": 2}
     def __getitem__(self, item):
         return self.Fruits[item]
Answered By: Luis Kleinwort

Expanding on @LuisKleinwort’s answer, if you want to do this for all class attributes:

fruits_dict = {'apple':0, 'banana':1}

class Fruits(object):
    def __init__(self, args):
        for k in args:
            setattr(self, k, args[k])
            
    def __getitem__(self, item):
        return getattr(self, item)

fruits = Fruits(fruits_dict)
print(fruits.apple)
print(fruits['apple'])
Answered By: muon

I do think you were asking about subscripting to a class not an instance of a class.

Here is my answer to this question: "How to create a subscriptable class in Python?"

class Subscriptable:
    def __class_getitem__(cls, item):
        return cls._get_child_dict()[item]

    @classmethod
    def _get_child_dict(cls):
        return {k: v for k, v in cls.__dict__.items() if not k.startswith('_')}


class Fruits(Subscriptable):
    Apple = 0
    Pear = 1
    Banana = 2

>>> Fruits['Apple']
    0
>>> Fruits['Pear']
    1
Answered By: Alexandre Mahdhaoui
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.