class declaration shortcut

Luis M. González luismgz at gmail.com
Thu Mar 1 15:21:53 EST 2007


On Mar 1, 3:03 pm, "Arnaud Delobelle" <arno... at googlemail.com> wrote:
> On Mar 1, 4:01 pm, Steven Bethard <steven.beth... at gmail.com> wrote:
>
> > Arnaud Delobelle wrote:
> [...]
> > This does pretty much the same thing as the recipe I posted:
>
> Not at all.  My new_struct create returns a new class which is similar
> to a C struct (notice the __slots__).  The recipe you refer to is
> nothing more a class which can be initialised with some attributes. It
> does not address the OP's question at all.
>
> >      http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502237
>
> > Note that your approach requires repetition of the 'Person' and quotes
> > around each attribute name, which the OP complained about. The recipe at
> > least gets rid of the repetition of 'Person'.
>
> The 'name' argument is not necessary.  It is just here to give a user-
> friendly name to the newly created class.
> One could just as well write the new_struct function so that
>
> >>> Person = new_struct('name', 'tel', ...)
>
> Of course it would be impossible for the function written as above to
> name the class in a user-meaningful way.  The recipe you refer to does
> not face this problem because it is not a method to quickly create a
> new class, it is merely a class whose __init__ method allows you to
> initialise some attribute at instance-creation time.
>
> As for the quotes around the attribute names, well... Let's say that
> if it was possible to do without, I don't think I would be using
> python...
>
> --
> Arnaud



This is the closest we got so far to the intended result.
If there was a way to enter attributes without quotes, it would be
almost identical.
Anyway, I wonder if the code comparison in www.rubuclr.com between the
c# and ruby code is fair...
As far as I know, classes and structs are not the same thing.
In the case of that ruby example, can someone add methods later?

Luis





More information about the Python-list mailing list