An empty object with dynamic attributes (expando)
Alf P. Steinbach
alfps at start.no
Thu Jun 3 17:36:36 EDT 2010
* dmtr, on 03.06.2010 23:00:
> How can I create an empty object with dynamic attributes? It should be
> something like:
>
>>>> m = object()
>>>> m.myattr = 1
>
> But this doesn't work. And I have to resort to:
>
>>>> class expando(object): pass
>>>> m = expando()
>>>> m.myattr = 1
>
> Is there a one-liner that would do the thing?
<example>
>>> m = lambda:0
>>> m.myattr = 1
>>> m.myattr
1
>>> _
</example>
But I feel that that is an abuse of the language, and that an Expando class like
you show is far better.
Thanks for the class name suggestion, by the way -- I've wondered what to call
such a class, and now it's clear. :-)
Cheers & hth.,
- Alf
--
blog at <url: http://alfps.wordpress.com>
More information about the Python-list
mailing list