69

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.

user805627
  • 4,247
  • 6
  • 32
  • 43
  • 4
    I have no answer for you, but I would like to know why? To me it looks like a regular Hash: >>> Fruit = {'Banana': 0, 'Apple': 1} >>> print Fruit['Banana'] 0 – Pengman Jul 13 '12 at 10:57
  • @Pengman I also want to implement 'Fruit' as a enum type. `if sth == Fruit.Apple: doSomething()`. – user805627 Jul 13 '12 at 11:13
  • 1
    Also, it seems to me that your real problem may require Python equivalent of "enums" and "constants" - tehre are several solutions for this in Python - check Pypi for "enum" - this one seems to be a favorite: http://pypi.python.org/pypi/flufl.enum/3.3.2 – jsbueno Jul 13 '12 at 12:09

5 Answers5

53

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]
Luis Kleinwort
  • 655
  • 5
  • 10
43

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
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    Thanks for your answer. Let me add some explanation: Class is also object in Python. The type of 'class object' is define by `__metaclass__`, default to `type`. So if you need a subscriptable class, just implement `__getitem__` in its `__metaclass__`. For more information about `__metaclass__`, read [this post](http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python?answertab=votes#tab-top). – user805627 Jul 13 '12 at 16:55
  • 2
    Note that [metaclasses are done differently in python 3](http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/). – Casey Kuball Aug 02 '12 at 15:31
  • 1
    @Darthfett The link fails now –  Jun 08 '15 at 07:16
  • @AbhiP Try the [webarchive version](http://web.archive.org/web/20140424195834/http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/), or [this useful link](http://www.pythoncentral.io/how-metaclasses-work-technically-in-python-2-and-3/) which shows the same code for both python 2 and 3 metaclasses. Looks like Mike Watkins has temporarily taken down his article. – Casey Kuball Jun 08 '15 at 17:46
9

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'])
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
muon
  • 12,821
  • 11
  • 69
  • 88
9

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

Here is my answer to the 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
0

Use self.__dict__.update(kwargs) & getattr(self, item).

class TrainingArgs:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
    def __getitem__(self, item):
        return getattr(self, item)

Test:

>>> args = TrainingArgs(learning_rate=1e-4, batch_size=8, epochs=10)
>>> args['learning_rate']
0.0001
>>> args['batch_size']
8
>>> args.epochs
10
Beyondo
  • 2,952
  • 1
  • 17
  • 42