An empty object with dynamic attributes (expando)
dmtr
dchichkov at gmail.com
Fri Jun 4 20:01:14 EDT 2010
> Why does it have to be a one-liner? Is the Enter key on your keyboard
> broken?
Nah. I was simply looking for something natural and intuitive, like: m
= object(); m.a = 1;
Usually python is pretty good providing these natural and intuitive
solutions.
> You have a perfectly good solution: define a class, then instantiate it.
> But if you need a one-liner (perhaps to win a game of code golf), then
> this will work:
>
> >>> m = type('', (), {})()
> >>> m.attribute = 2
Heh. Creating it dynamically. Ace. ;)
-- Cheers, Dmitry
More information about the Python-list
mailing list