__getitem__ on new-style classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • simon@arrowtheory.com

    #1

    __getitem__ on new-style classes

    What i'm trying to do is tie special methods of a "proxy" instance
    to another instance:

    def test1():
    class Container:
    def __init__( self, data ):
    self.data = data
    self.__getitem_ _ = self.data.__get item__

    data = range(10)
    c = Container(data)
    print "test1"
    print c[3]

    This works OK. But when I try the same thing on
    new style classes:

    def test2():
    print "test2"
    class Container(objec t):
    def __init__( self, data ):
    self.data = data
    # self.__dict__["__getitem_ _"] = self.data.__get item__
    self.__setattr_ _( "__getitem_ _", self.data.__get item__ )
    data = range(10)
    c = Container(data)
    print
    print c.__getitem__(3 ) # works OK
    print c[3] # fails

    I get a "TypeError: unindexable object". It
    seems that c[] does not call __getitem__ in this case.

    The plot thickens, however, when one tries the following:

    def test3():
    data = range(10)
    c = type( "Container" , (), { "__getitem__":d ata.__getitem__ } )()
    print "test3"
    print c[3]

    Which works fine. However, if I need to resort to
    such trickery, no-one here where i work will have any idea
    what it does :)

    Would anyone like to shed some light on what is going on here ?

    bye,

    Simon.

  • Michael Spencer

    #2
    Re: __getitem__ on new-style classes

    simon@arrowtheo ry.com wrote:[color=blue]
    > What i'm trying to do is tie special methods of a "proxy" instance
    > to another instance:[/color]
    ....[color=blue]
    > new style classes:
    >
    > def test2():
    > print "test2"
    > class Container(objec t):
    > def __init__( self, data ):
    > self.data = data
    > # self.__dict__["__getitem_ _"] = self.data.__get item__
    > self.__setattr_ _( "__getitem_ _", self.data.__get item__ )
    > data = range(10)
    > c = Container(data)
    > print
    > print c.__getitem__(3 ) # works OK
    > print c[3] # fails[/color]

    [color=blue]
    >
    > I get a "TypeError: unindexable object". It
    > seems that c[] does not call __getitem__ in this case.[/color]
    because __getitem__ is looked up in the class, not the instance dictionary[color=blue]
    >
    > The plot thickens, however, when one tries the following:
    >
    > def test3():
    > data = range(10)
    > c = type( "Container" , (), { "__getitem__":d ata.__getitem__ } )()
    > print "test3"
    > print c[3][/color]

    Here you've created an entry in the class dictionary, just like writing
    data = range(10)
    class Container(objec t):
    __getitem__ = data.__getitem_ _

    but, this probably doesn't help you much if you want the method to delegate to
    an attribute of the instance, since the instance is unavailable at class
    definition time

    So, one solution, is to use create a delegating descriptor, like so:

    class Delegate(object ):
    def __init__(self, attrname):
    self.attrname = attrname
    def __get__(self, obj, cls):
    if isinstance(obj, cls):
    # Note that the attribute is looked up on obj._data
    return getattr(obj._da ta, self.attrname)
    else:
    return self

    class Example(object) :
    __getitem__ = Delegate("__get item__")

    def __init__(self, delegate):
    self._data = delegate
    [color=blue][color=green][color=darkred]
    >>> e = Example(range(1 0))
    >>> e[3][/color][/color][/color]
    3


    Michael


    Comment

    Working...