0

I made a simple Generic number class(actually function).

It returns the NewClass based input digits.

example) each line means the new class based on each digits.

def create_number_class(alphabet):
    class NewClass(object):
        pass
    return NewClass

almost code was deleted for your readability

BinClass = create_number_class("01")
DecimalClass = create_number_class("0123456789")
x = BinClass("100")
y = DecimalClass("3942")

in that case, the problem occurrs when print type(x)

when running code below, it

print(type(x))
print(type(y))

the output is same

<class '__main__.create_number_class.<locals>.NewClass'>
<class '__main__.create_number_class.<locals>.NewClass'>

I want to make them different. So, I changed the code as below, it doesn't work. So, I don't know how to to it. Always thank you all, lot of help to me.

def create_number_class(alphabet):
    class NewClass(object):
        pass
    NewClass.__name__ = alphabet # input data
    return NewClass
frhyme
  • 966
  • 1
  • 15
  • 24
  • It’s generally not a good idea changing the `type` of an object, you should instead look into changing the `__str__` and/or `__repl__` of the object – Taku Jun 07 '17 at 05:51
  • 2
    Possible duplicate of [changing "type" of python objects](https://stackoverflow.com/questions/15404256/changing-type-of-python-objects) – Taku Jun 07 '17 at 05:54
  • Thank you all. it helps me a lot – frhyme Jun 07 '17 at 07:59

0 Answers0