Creating my own integer object in Python

Essentially, I want to be able to do something like:

a = Integer(1) a += 1 print a 

And of course, print number two as the result. What methods do I need to create to get this behavior in my Integer class?

Disclaimer: I do not plan to use this for the "real", just curious.

+6
source share
6 answers

This is a simple and incomplete example. Take a look at the __sub__ , __div__ methods, etc.

 class Integer(object) : def __init__(self, val=0) : self._val = int(val) def __add__(self, val) : if type(val) == Integer : return Integer(self._val + val._val) return self._val + val def __iadd__(self, val) : self._val += val return self def __str__(self) : return str(self._val) def __repr__(self) : return 'Integer(%s)' %self._val 

Then

 n = Integer() print n m = Integer(7) m+=5 print m 

__repr__ fixed by __repr__ and added by __iadd__ . Thanks to @Keith for pointing out issues. EDIT Fixed __add__ to allow adding between integers.

+8
source

First, quickly review the documentation in the reference guide for emulating numeric types .

(Don't get too hung up on this - it just gives you some idea of ​​the methods that underlie arithmetic operations in Python)

Then refer to the documentation for the numbers module, which includes all the abstract base classes that are most important for emulating various types of numbers (e.g. numbers.Integral for custom integers).

+6
source

You can use operator overloading :

 class Integer: def __init__(self, value): self.value = value def __repr__(self): return str(self.value) def __add__(self, value): self.value += value return self a = Integer(2) print a a = a+3 print a a += 4 print a 
+3
source

I assume that you want your Integer class to change. To get your example, this will work:

 class Integer(object): def __init__(self, num): self._val = num def __iadd__(self, other): self._val += int(other) def __str__(self): return str(self._val) 
+3
source

If you want to overload the default cast-to-string operators, the phrase you are looking for is β€œmagic methods”. These are methods named " __<name>__ " and are used by python in cases other than direct method calls. You would like to define the __add__ and __str__ for your class to work with lines 2 and 3.

It is worth noting that the __add__ method will be called if your new type is a left operand, and any type can be passed as its argument. For cases where your is the correct operand, you must also define the __radd__ method. This applies to all binary operators.

For a more complete list of magic methods for a numeric type, see Emulating Numeric Types .

+3
source
 class Integer(object): def __init__(self, value=0): self._value = int(value) def __add__(self, other): if isinstance(other, Integer): return Integer(self._value + other._value) return Integer(self._value + other) def __iadd__(self, other): if isinstance(other, Integer): self._value += other._value else: self._value += other return self def __sub__(self, other): if isinstance(other, Integer): return Integer(self._value - other._value) return Integer(self._value - other) def __isub__(self, other): if isinstance(other, Integer): self._value -= other._value else: self._value -= other return self def __mul__(self, other): if isinstance(other, Integer): return Integer(self._value * other._value) return Integer(self._value * other) def __div__(self, other): if isinstance(other, Integer): return Integer(self._value / other._value) return Integer(self._value / other) def __str__(self): return str(self._value) def __int__(self): return self._value def __float__(self): return float(self._value) def __repr__(self): return 'Integer(%s)' % self._value 
+1
source

Source: https://habr.com/ru/post/885560/


All Articles