I have a list of classes, and I'm trying to dynamically create another list of classes, such that each one has a classmethod that creates an instance of a child of a class from the original list.
The problem is that all the methods end up returning an instance of the same class (the last one created).
Here is a minimized version of my code:
class C1():
var = 1
class C2():
var = 2
parents = (C1, C2)
children = []
wrappers = []
for c in parents:
class ChildClass(c):
pass
children.append(ChildClass())
class Wrapper():
@classmethod
def method(cls):
cls.wrapped = ChildClass()
wrappers.append(Wrapper)
print(list(child.var for child in children))
for wrapper in wrappers:
wrapper.method()
print(list(wrapper.wrapped.var for wrapper in wrappers))
The output:
[1, 2]
[2, 2]
You can see that the children list contains different instances, while the classmethod creates an instance of a child of C2 in both cases.
How can I fix my code so that each classmethod creates an instance of the correct class?
(I'm using python 2.7.4)
unittest. I have a base tester class, and I'm creating a child class for each browser (Firefox, IE, etc...). Each child class needs asetUpClass(which is aclassmethod) that creates an instance of a seleniumWebDriverfor its browser.