i have a python class like so:
class TAG_Short(NBTTag):
def __init__(self, value=None):
self.name = None
self.value = value
def __repr__(self):
return "TAG_Short: %i" % self.value
This tag is filled out at runtime, but i’d also like to be able to use it like:
mytag = TAG_Short(3)
mycalc = 3 + ( mytag % 2) / mytag
is there any method i need to add to the tag to allow me to use it as a valid numeric type?
I see– what you would like is to have something like a
__as_number__method you can define inTAG_Short, which would allow you to return a number which is then used in any place where aValueErrorwould 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 theTAG_Shortobject.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:
Then you could do something like: