10

I would like to be able to add to a custom class in the style of:

x=myclass("Something", 7)
x + 3

7, of course, corresponds with an inner property that I'd like to increment by adding to it.

The class holds a number that refers to a location in a list. This might seem like something that can be done by a normal integer, but I need it to act as a separate type. This is all done to emulate an old game language. The class is its 'variable' class, and the value of the variable is stored in the aforementioned list. Apparently, on older version of the game, arrays were faked by doing math on the variable object instance to grab a different variable. So I'm trying to emulate that.

3
  • The class holds a number that refers to a location in a list. This might seem like something that can be done by a normal integer, but I need it to act as a separate type. This is all done to emulate an old game language. The class is its 'variable' class, and the value of the variable is stored in the aforementioned list. Apparently, on older version of the game, arrays were faked by doing math on the variable object instance to grab a different variable. So I'm trying to emulate that @.@ Commented Mar 23, 2012 at 13:11
  • Still not sure what the requirements are, but from this comment it seems easiest to dervie from int. Commented Mar 23, 2012 at 13:16
  • @Kelketek: I've shifted your comment into the question, which is where it belongs. Commented Mar 23, 2012 at 13:16

2 Answers 2

15

If you want to support addition for class instances, you need to define an __add__() method on your class:

class MyClass(object):
    def __init__(self, x):
        self.x = x
    def __add__(self, other):
        return self.x + other

Example:

>>> a = MyClass(7)
>>> a + 3
10

To also support 3 + a, define the __radd__() method.

If you want to be able to update the x attribute of MyClass instances using

a += 3

you can define __iadd__().

If you want class instances to behave like integers with some additional methods and attributes, you should simply derive from int.

Sign up to request clarification or add additional context in comments.

3 Comments

This works. I don't want all of int's functionality. For instance, I don't want radd to work for this case. Is there a listing of functions somewhere that would show me what I can implement? Do I just need to add in methods from the int class on my class for the ones I want to support?
@Kelketek: Just follow any of the links in the post for further magic methods. I don't understand your second question.
I love Python because of features like these. You can pretty much do anything in Python.
0

What you're looking to do is operator overloading. You can do this in python new style classes by overloading the __add__ method like so:

>>> class Test(object):
...     def __init__(self): self.prop = 3
...     def __add__(self, x): 
...         return self.prop + x
... 
>>> Test() + 4
7
>>> 

1 Comment

Rereading what I've written, I probably shouldn't be modifying the prop property when doing addition. Whoops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.