You may inherit numbers.Number:
def baseN(base,alphabet='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'):
class _baseN(numbers.Number):
digits=alphabet[:base]
def __init__(self,value):
if isinstance(value,int):
self.value=value
if self.value==0:
self.string='0'
else:
tmp=[abs(value)]
while tmp[0]!=0:
tmp[:1]=divmod(tmp[0],base)
tmp=[alphabet[i] for i in tmp]
tmp[0]='-' if self.value<0 else ''
self.string=''.join(tmp)
elif isinstance(value,str):
assert(value.isalnum())
self.string=str(value)
self.value=0
for d in value:
self.value=self.value*base+self.digits.index(d)
else:
self.value=0
self.string='0'
def __int__(self):
return self.value
def __str__(self):
return self.string
def __repr__(self):
return self.string
def __add__(self,another):
return self.__class__(self.value+int(another))
return None if base>len(alphabet) else _baseN
Found another bug. Change it to a factory function. Now may handle general situation.