0

I have a lengthy, repetitive code full of classes where they each do almost the same thing, and I was trying to do something like this to shorten it:

def newClass(cName, para):
    string = '''class '''+cName+'''(str):
    def __new__(cls, num): 
        return(str.__new__(cls, func('''+para+''')))'''
    exec(string)

but the new class is neither a part of newClass or defined on its own, how can I make it so it creates the new class as if it were not defined in a function? If that's impossible, how can I make it so that I can do something like:

newClass('Fun', 'p')
newClass.Fun('inp')

Edit: I am making a base conversion program, and wanted to be able to do things that python does not do like bin(a) + bin(b). Here is a simplified version of one of my classes:

class Bin(str):
    '''Binary (base 2) number'''
    def __new__(cls, num): 
        return(str.__new__(cls, baseconv(num, 2, '01')))
    def __int__(self): 
        return(intconv(self, 2, '01'))
    def __add__(a, b): 
        return(Bin(a.__int__() + b.__int__()))
    def __sub__(a, b): 
        return(Bin(a.__int__() - b.__int__()))
    ...

class Ter(str):
    '''Ternary (base 3) number'''
    def __new__(cls, num): 
        return(str.__new__(cls, baseconv(num, 3, '012')))
    def __int__(self): 
        return(intconv(self, 3, '012'))
    def __add__(a, b): 
        return(Ter(a.__int__() + b.__int__()))
    def __sub__(a, b): 
        return(Ter(a.__int__() - b.__int__()))
    ...

Where baseconv and intconv are two functions I've defined previously.

diligar
  • 413
  • 5
  • 11
  • 2
    Consider whether constructing a class using [the `type` function](https://docs.python.org/2/library/functions.html#type) may work instead in your case. Using exec/eval is strongly discouraged, and is rarely a suitable design. (Generating classes in general is slightly frowned-upon, but if it's neccessary then using `type` may be reasonable.) – Jeremy Mar 25 '16 at 23:36
  • 2
    @JaredGoguen -- `eval` won't work here. It only works on expressions and class definition is definitely not an expression. – mgilson Mar 25 '16 at 23:47
  • @diligar -- Can you show us a simplified example of some "repetitive code full of classes" that you would like to refactor? That may help us to give better suggestions (e.g. using `type` as suggested by Jeremy). – mgilson Mar 25 '16 at 23:49
  • Why do you have so many classes that do almost the same thing? It sounds like you might be overusing inheritance. – user2357112 Mar 25 '16 at 23:50
  • @JaredGoguen I tried that, but I figured `exec` would be a better idea after looking at [this question](http://stackoverflow.com/questions/5920120/how-to-define-a-function-from-a-string-using-python) – diligar Mar 25 '16 at 23:51
  • "I am making a base conversion program" The base should be a field, not a completely different class. Also bases only matter when converting to a string; you should be fine using `int` class plus a few base conversion functions. – Colonel Thirty Two Mar 26 '16 at 00:04

0 Answers0