I am writing a Java collection class that is meant to be used with Jython. I want end users to be able to manipulate the collection this way:
myCollection = MyJavaCollection()
myCollection[0] += 10.;
a = myCollection[0]; //a = 10
myCollection[0] += 20.;
b = myCollection[0]; //b = 20
What I find in the Python documentation is the following methods:
__getitem__ and __setitem__ methods should do the job for bracket operator overloading.
__iadd__ method is the good candidate for the +=.
How can I mix both to do want I want ?
Note that myCollection[0] += 10.; will really be interpreted something like:
myCollection.__setitem__(0,  myCollection.__getitem__(0).__iadd__(10.))
Therefore, to make this work you need to implement:
__getitem__ and __setitem__ on MyJavaCollection; and__iadd__ (or __add__, which Python will fall back to if __iadd__ isn't implemented) on whatever .__getitem__ is going to return, not MyJavaCollection itself - if it will return something that already implements addition, like the floats in your example, you're fine.A quick demonstration:
>>> class Container(object):
    def __init__(self, contained):
        self.contained = contained
    def __getitem__(self, key):
        print "Container.__getitem__"
        return self.contained
    def __setitem__(self, key, val):
        print "Container.__setitem__"
        self.contained = val
>>> class Contained(object):
    def __add__(self, other):
        print "Contained.__add__"
        return "foo"
>>> test = Container(Contained())
>>> test[0] += 1
Container.__getitem__
Contained.__add__
Container.__setitem__
>>> test.contained
'foo'
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With