class declaration shortcut

Arnaud Delobelle arnodel at googlemail.com
Thu Mar 1 13:03:34 EST 2007


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




More information about the Python-list mailing list