I see-- what you would like is to have something like a __as_number__
method you can define in TAG_Short
, which would allow you to return a number which is then used in any place where a ValueError
would be about to be raised. I have no idea if there is any way to do something like that, short of implementing that metafeature yourself.
What you can do is define __add__
, __radd__
, __mul__
, __rmul__
, etc (you must define every numeric method if you want your object to truly behave like a number in every situation), and have each of them return the result of doing the desired operation with what you consider to be the number representation of the TAG_Short
object.
If you find yourself doing this often enough, you may consider implementing the metafeature you describe (or first looking for a stable implementation to reuse). It would be quite feasible in Python. I think it might even be as easy as a good-old-fashioned class to be inherited from (untested code follows), with something kind of like:
class AbstractNumberImpersonator:
# child classes should define method .to_number()
def __add__( self, other ):
return self.to_number() + other
__radd__ = __add__
def __mul__( self, other ):
return self.to_number() * other
__rmul__ = __mul__
# etc - implement all the others in the same fashion
Then you could do something like:
class TAG_Short(NBTTag,AbstractNumberImpersonator):
def __init__(self, value=None):
self.name = None
self.value = value
def __repr__(self):
return "TAG_Short: %i" % self.value
def to_number(self):
return self.value