[ACCEPTED]-does python have conversion operators?-operators

Accepted answer
Score: 12

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 11 more information search for "radd" in 10 the following docs:

You'll find other such 9 methods for "right addition", "right 8 subtraction", etc.

Here's another link 7 covering the same operators:

By the way, Python 6 does have casting operators:

But, they won't 5 accomplish what you need in your example 4 (basically because methods don't have any 3 type annotation for parameters, so there's 2 no good way to cast implicitly). So you 1 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
Score: 5

You won't get conversion operators like 9 in C++ because Python does not have this 8 kind of strong static type system. The only 7 automatic conversion operators are those 6 which handle default numeric values (int/float); they 5 are predefined in the language and cannot 4 be changed.

Type "conversion" is usually 3 done by constructors/factories. You can 2 then overload standard methods like __add__ to 1 make it work more like other classes.

Score: 3

Is this 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]:

0

Score: 3

sometimes maybe just subclass from int directly 1 is enough. then __add__ and __radd__ need not costuming.

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
Score: 2

Sorry for coming to the party 8.5 years 12 late. You can derive from an immutable (ie. int). You 11 cannot define __init__ because the immutable is 10 already created and can't be modified (by 9 definition). This is where __new__ comes 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 8 answer your question about conversion operators. You 7 can also define __int__, __long__, __float__, and obviously, __str__. To 6 convert or cast to an arbitrary object, you 5 will most likely need to modify the other 4 object to get what you want. You can use 3 the __new__ method of that other object. Or if 2 the other object is already created, try 1 using __call__.

More Related questions