class declaration shortcut
BJörn Lindqvist
bjourne at gmail.com
Thu Mar 1 06:15:21 EST 2007
On 28 Feb 2007 13:53:37 -0800, Luis M. González <luismgz at gmail.com> wrote:
> Hmmm... not really.
> The code above is supposed to be a shorter way of writing this:
>
> class Person:
> def __init__(self, name, birthday, children):
> self.name = name
> self.birthday = birthday
> self.children = children
>
> So the purpose of this question is finding a way to emulate this with
> a single line and minimal typing.
I believe this is what you are looking for:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/361668
olle = attrdict(name = "Olle", birthday = date(2222, 12, 1), children = 329)
print olle.name, olle.birthday, olle.children
It is not identical to the Ruby recipe, but is IMHO better. Being able
to instantiate objects on the fly, without having to explicitly
declare the class, is a big advantage.
--
mvh Björn
More information about the Python-list
mailing list