In C, if I want to define a type from a name I could use the preprocessor. For example,
#define DEFINE_STRUCT(name) \
struct My##name##Struct \
{ \
int integerMember##name; \
double doubleMember##name; \
}
And then I could define a concrete struct like so
DEFINE_STRUCT(Useless);
and use the Useless
struct like this
struct MyUseslessStruct instance;
So my question is
- Is there a way to achieve this in Python?
I have the following class
class ClassName(SQLTable):
items = []
def __init__(self, value):
SQLTable.__init__(self)
# some common code
if value in self.items:
return
self.items.append(value)
For each ClassName
the contents of items
will be different, so I would like something like
def defineclass(ClassName):
class <Substitute ClassName Here>(SQLTable):
items = []
def __init__(self, value):
SQLTable.__init__(self)
# some common code
if value in self.items:
return
self.items.append(value)
I don't want to repeat the code over and over, I would like to generate it if possible.