I have a situation where I'm making a bunch of classes, and a bunch of them are really basically the same, so I'd like to make them in a loop. They are being used with a registration system, so there's no problem one the USAGE side, but I'm not sure how to actually define a class with a variable determining the class name...
Simple example:
classList = ['foo', 'bar', 'baz']
for className in classList:
class {{{className}}}_calc(BaseCalc):
def __init__ (self, dataFrame):
self.column = dataFrame[className]
def calc ():
return self.column.sum()
This is a very simplified case, obviously. I can't change the arguments to init because there are a whole bunch of these already existing, that are part of a larger structure.
The rest of the example is using pandas syntax, just to give an idea of how this is being used... but it's actually being used with a SQL DB, and is much more complicated... I just don't want to have to defend "why are you doing this in the first place?" I have good reasons, leave it at that.
classname is in {{{ }}} in the class line to denote that it's a variable, and not actually syntactically correct there. The question is "how do I denote what I have used {{{ }}} for?" I think.
The answer may be metaclasses, but I'm still not sure how to make the NAME of my class variable....
ETA: Trying to use @python_user answer:
classList = ['foooo', 'bar', 'baaz']
class Base ():
def __init__ (self, buq):
self.buq = buq
def getBuq(self):
return 'buq: ' + self.buq
for cls in classList:
class TEMP(Base):
className = cls
def __init__ (self):
self.qux = len(cls)
Base.__init__(self, cls)
def blee(self, inpt):
return inpt+ self.qux
TEMP.__name__ = f'{cls}'
TEMP.__qualname__ = f'{cls}'
globals()[cls] = TEMP
f = foo()
f.getBuq()
>>>> 'buq: baaz'
This is only giving me the baaz class. All three are giving baaz... Am I doing something really dumb?
question from:
https://stackoverflow.com/questions/65888722/can-you-make-classes-in-a-loop-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…