I just made a generic number class:

  • From any ordered character list, make number class representing that ordered character list.
  • create_number_class("01") returns binary number class
  • create_number_class("0123456789") returns decimal number class
  • create_number_class("abcdefghij") return decimal number class but representing each digit as a alphabet.

Here is the generic number class definition. I think it is well-made class definition. Is there something needed for improvement in that class definition?


ABC_Class = create_number_class("abc")
x = ABC_Class("baa")
y = ABC_Class("bbb")
#output digits: abc, v: cbb, decimal_v: 22

Here is the class definition:

def create_number_class(alphabet):
    class temp(object):
        digits = alphabet
        def __init__(self, v):
            self.v = v
            self.decimal_v = self.to_decimal(self)
        def to_decimal(self):
            r = 0
            for i in range(0, len(self.v)):
                r += len(temp.digits)**(len(self.v)-i-1)*(temp.digits.index(self.v[i]))
            return r
        def from_decimal(cls, decimal_v):
            r = []
            mod = len(temp.digits)
            if decimal_v < mod:
                return cls(temp.digits[decimal_v])
            while True:
                remainder = decimal_v % mod
                decimal_v = int((decimal_v - remainder)/ mod)
                if decimal_v < mod:
            r = "".join(list(reversed([temp.digits[x] for x in r])))
            #r = "".join(list(reversed([str(temp.digits.index(str(x))) for x in r])))
            return cls(r)
        def __add__(self, other):
            return temp.from_decimal(self.decimal_v+other.decimal_v)
        def __sub__(self, other):
            return temp.from_decimal(self.decimal_v-other.decimal_v)
        def __mul__(self, other):
            return temp.from_decimal(self.decimal_v*other.decimal_v)
        def __floordiv__(self, other):
            return temp.from_decimal(self.decimal_v//other.decimal_v)
        def __str__(self):
            return "digits: {}, v: {}, decimal_v: {}".format(temp.digits, self.v, self.decimal_v)
        def convert_to(self, new_class):
            return new_class.from_decimal(self.decimal_v)
    return temp

Here is an example:

BinClass = create_number_class("01")
DecimalClass = create_number_class("0123456789")

x = BinClass("111")
x = BinClass("1000")
y = BinClass("10")

HexClass = create_number_class('0123456789ABCDEF')
x = HexClass('1')
y = HexClass('AA')

isinstance(x, BinClass)

Your code looks very good for me, what I found are just some minor improvements:

  1. You do not need to inherit from object so you can just do class temp:
  2. I would suggest to change temporary class name to something else since when you do print(type(x)) I would rather see something more informative than <class '__main__.create_number_class.<locals>.temp'>. I would suggest to add another argument for your method called className and set it dynamically as shown here
  3. New lines after each method would be appreciated since it would make code more readable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.