Just asking this question because it seems to be a pattern that emerges quite regularly, and I'm wondering if anybody is aware of a standard library feature that supports this, or a one-liner perhaps that does something equivalent.
If I wanted to define a class for passing around data, that can contain arbitrary attributes, to be set when constructing I could do something like this:
>>> class Splat:
def __init__(self, **args):
for k, v in args:
setattr(self, k, v)
and then use it like this:
>>> s = Splat(attr1='burp', attr2='gurt', attr3='sint')
>>> vars(s)
{'attr2': 'gurt', 'attr3': 'sint', 'attr1': 'burp'}
It's a pattern I've seen used in the python standard library (ssl.wrapSocket
or something in there if memory serves) and I'd imagine it would be useful enough in general that there could be a commonly used python idiom or mixin that I could leverage rather than having to create my own one whenever I need it.
I know it's only a four liner to include this when I need, but in the interests of parsimony I'd like to know if there's a 0 or at least 1-liner variant I could use. Like this maybe:
s = TheBaseClass({'attr2': 'gurt', 'attr3': 'sint', 'attr1': 'burp'})
or
class Splat(TheBaseClass):
pass
Some might be quick to jump in and say "use a dict" instead, but I'm thinking of usecases where one might wish to mock an existing object-type (duck typing) and also to infer that it is a specific "type" (like if this feature were just used as a mixin).
Edit: Similarly, an issue with a construction such as s = type('Splat', (), {'attr2': 'gurt', 'attr3': 'sint', 'attr1': 'burp'})
is that I don't get to declare it, or get much in the way of type safety ...