'Pass class name as a variable in Python [closed]
I have the below sample code:
def sample(cls):
class a():
def __init__(self):
self.value='a'
class b():
def __init__(self):
self.value='b'
def __init__(self):
self.cls=cls()
return self.value
sample(b)
I have defined 2 classes in the sample code. I want to be able to define the class name in a function. So the above code will return 'b'. Is there a way to do it? The above code will give me the error:
'str' object is not callable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
