What I want to do in my code:
myobj = <SomeBuiltinClass>()
myobj.randomattr = 1
print myobj.randomattr
...
I can implement a custom SomeClass that implements __setattr__
__getattr__
.
But I wonder if there is already a built-in Python class or simple way to do this?
You can just use an empty class:
class A(object): pass
a = A()
a.randomattr = 1
I like using the Bunch idiom for this. There are list of variations and some discussion here.
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