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
3
  • 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 Commented Jun 7, 2017 at 5:51
  • 2
    Possible duplicate of changing "type" of python objects Commented Jun 7, 2017 at 5:54
  • Thank you all. it helps me a lot Commented Jun 7, 2017 at 7:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.