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.

asked Mar 25, 2016 at 23:34
6
  • 2
    Consider whether constructing a class using the type function 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.) Commented Mar 25, 2016 at 23:36
  • 2
    @JaredGoguen -- eval won't work here. It only works on expressions and class definition is definitely not an expression. Commented Mar 25, 2016 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). Commented Mar 25, 2016 at 23:49
  • Why do you have so many classes that do almost the same thing? It sounds like you might be overusing inheritance. Commented Mar 25, 2016 at 23:50
  • @JaredGoguen I tried that, but I figured exec would be a better idea after looking at this question Commented Mar 25, 2016 at 23:51

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.