Present Scenario :
I have a set of classes that all take a common argument context in constructor, and all the classes inherit from a common base.
class base:
def common_method(self):
pass
class a(base):
def __init__(self,context, aa):
pass
class b(base):
def __init__(self, context, bb):
pass
# ...
class z(base):
def __init__(self, context, zz):
pass
Usage in main:
context = get_context_from_some_method()
A = a(context, 1)
B = b(context, 2)
C = c(context, 3)
D = d(context, 4)
.
.
.
Z = z(context, 26)
Problem:
- Is there a tricky way possible to supply
contextto all the classes implicitly ? - We have a common base class, but I don’t see a obvious way to let it set the context for all the classes.
- It can be assumed that I want to keep context common to all my class instances.
- Just a random thought – Can meta class help somehow ?
I know it seems silly, but I just want to get rid of the redundancy in some way, so that I can set a global context somehow and concentrate on my objects.
Please suggest some way around ?
** Update to Question **
I can-not set a context in the base class, since this is to be used in a web application. So, many pages with different context will be using the class structure. So, If I set a context in base then it will conflict to the context that will be set by another instance of webpage that will use the same base. Since, in a web-application all the above classes will be in memory common to all pages.
Edit: If you don’t want to / can’t use a class variable, your best bet is to use a factory function. Either make it a static method on the base class, or a module level function.
Alternatively, and I don’t know if this works in your situation, just use a module level global variable:
In addition to the advice to use class variables from the other answer, I advise you to copy the context onto the instances, so that you can later change the context without affecting already created instances.