Does python have conversion operators?

I don’t think so, but I thought I would ask just in case. For example, for use in a class that encapsulates an int:

i = IntContainer(3) i + 5 

And I'm not just interested in this int example, I was looking for something clean and general, rather than overriding each int and string method.

Thanks sunqiang. This is what I wanted. I did not understand that you can subclass these immutable types (coming from C ++).

 class IntContainer(int): def __init__(self,i): #do stuff here self.f = 4 def MultiplyBy4(self): #some member function self *= self.f return self print 3+IntContainer(3).MultiplyBy4() 
+7
operators python
source share
5 answers

sometimes, perhaps just a subclass from int is enough. then __add__ and __radd__ do not require a costume.

 class IntContainer(int): pass i = IntContainer(3) print i + 5 # 8 print 4 + i # 7 class StrContainer(str): pass s = StrContainer(3) print s + '5' # 35 print '4' + s # 43 
+3
source share

This should do what you need:

 class IntContainer(object): def __init__(self, x): self.x = x def __add__(self, other): # do some type checking on other return self.x + other def __radd__(self, other): # do some type checking on other return self.x + other 

Output:

 In [6]: IntContainer(3) + 6 Out[6]: 9 In [7]: 6 + IntContainer(3) Out[7]: 9 

For more information, find "radd" in the following documents:

You will find other such methods for “correct addition,” “right subtraction,” etc.

Here's another link covering the same operators:

By the way, Python has cast operators:

But they will not do what you need in your example (mainly because methods do not have type annotations for parameters, so there is no way to do it implicitly). So you can do this:

 class IntContainer2(object): def __init__(self, x): self.x = x def __int__(self): return self.x ic = IntContainer2(3) print int(ic) + 6 print 6 + int(ic) 

But this will fail:

 print ic + 6 # error: no implicit coercion 
+11
source share

That's what you need?

 In [1]: class IntContainer(object): ...: def __init__(self, val): ...: self.val = val ...: def __add__(self, val): ...: return self.val + val ...: def __radd__(self, val): ...: return self.val + val ...: ...: In [2]: i = IntContainer(3) In [3]: i + 5 Out[3]: 8 In [4]: 
+3
source share

You won’t get conversion operators like in C ++, because Python doesn’t have such a strong static type system. The only automatic conversion operators are those that handle numeric defaults (int / float); they are predefined in the language and cannot be changed.

The conversion type is typically performed by designers / factories. Then you can overload standard methods like __add__ that they work more than other classes.

+3
source share

Sorry to be at the party for 8.5 years. You can get immutable (i.e. int). You cannot define __init__ because immutable is already created and cannot be changed (by definition). Here __new__ in handy.

 class IntContainer(int): def __new__ (cls, val): ival = int.__new__(cls, val) ival._rval = 'IntContainer(%d)' % ival return ival def __repr__ (self): return self._rval In [1]: i = IntContainer(3) In [2]: i Out[2]: IntContainer(3) In [3]: repr(i) Out[3]: 'IntContainer(3)' In [4]: str(i) Out[4]: '3' In [5]: i + 5 Out[5]: 8 In [6]: 4 + i Out[6]: 7 In [7]: int(i) Out[7]: 3 In [8]: float(i) Out[8]: 3.0 

Now to answer your question about conversion operators. You can also define __int__ , __long__ , __float__ and obviously __str__ . To convert or translate an arbitrary object, you will most likely need to change another object to get what you want. You can use the __new__ method of this other object. Or, if another object has already been created, try using __call__ .

0
source share

All Articles