To create a property in a class you simply do self.property = value
. I want to be able to have the properties in this class completely dependent on a parameter. Let us call this class Foo
.
instances of the Foo
class would take a list of tuples:
l = [("first","foo"),("second","bar"),("anything","you get the point")]
bar = Foo(l)
now the instance of the Foo
class we assigned to bar
would have the following properties:
bar.first
#foo
bar.second
#bar
bar.anything
#you get the point
Is this even remotely possible? How?
I thought of another answer you could use using type()
. It's completely different to my current answer so I've added a different answer:
>>> bar = type('Foo', (), dict(l))()
>>> bar.first
'foo'
>>> bar.second
'bar'
>>> bar.anything
'you get the point'
type()
returns a class, not an instance, hence the extra ()
at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With