Re: __getitem__ and classmethod/staticmethod

Discussion in 'Python' started by Andrew Bennetts, Jun 25, 2004.

  1. On Fri, Jun 25, 2004 at 10:18:53AM -0700, Karl Chen wrote:
    >
    > Hi.
    >
    > Is it possible to make a classmethod out of __getitem__?
    >
    > I.e. I want to do:
    >
    > class C:
    > def __getitem__(p):
    > return 1+p
    >
    > __getitem__ = classmethod(__getitem__)
    >
    > So that C[3] works (in addition to C()[3]). But I always get
    > TypeError: unsubscriptable object


    (You seem to be confusing classmethod and staticmethod, but I'm guessing
    that's just a typo in your definition of __getitem__)

    The problem is that a __getitem__ in a class's __dict__ has nothing to do
    with the operators defined on that class.

    To define the __getitem__ operator on a class object, you need to define it
    on the class's class, i.e. its metaclass;

    >>> class SubscriptableType(type):

    .... def __getitem__(self, x):
    .... return x+1
    ....
    >>> class C(object):

    .... __metaclass__ = SubscriptableType
    ....
    >>> C[3]

    4

    Of course, this doesn't define it for instances of C:

    >>> C()[3]

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unindexable object

    So to do both you'd have to do something like:

    >>> class C(object):

    .... __metaclass__ = SubscriptableType
    .... def __getitem__(x):
    .... return x+1
    .... __getitem__ = staticmethod(__getitem__)
    ....
    >>> C[3]

    4
    >>> C()[3]

    4

    To avoid redefining the same method twice, you could something like this:

    def getitem(x):
    return x + 1

    class SubscriptableType(type):
    __getitem__ = staticmethod(getitem)

    class C(object):
    __metaclass__ = SubscriptableType
    __getitem__ = staticmethod(getitem)

    -Andrew.
     
    Andrew Bennetts, Jun 25, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KanZen

    __getitem__ and arguments

    KanZen, Jul 19, 2003, in forum: Python
    Replies:
    4
    Views:
    785
    Ben Finney
    Jul 19, 2003
  2. Karl Chen
    Replies:
    0
    Views:
    357
    Karl Chen
    Jun 25, 2004
  3. Steven Bethard

    unittest.TestCase, lambda and __getitem__

    Steven Bethard, Sep 13, 2004, in forum: Python
    Replies:
    7
    Views:
    473
    Alex Martelli
    Sep 14, 2004
  4. C Gillespie

    staticmethod and classmethod

    C Gillespie, May 24, 2005, in forum: Python
    Replies:
    5
    Views:
    5,786
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    May 26, 2005
  5. Bruno Desthuilliers

    classmethod & staticmethod

    Bruno Desthuilliers, Jul 21, 2007, in forum: Python
    Replies:
    16
    Views:
    623
    Steven D'Aprano
    Jul 26, 2007
Loading...

Share This Page