initializing mutable class attributes

Alex Martelli aleaxit at yahoo.com
Fri Sep 3 04:16:30 EDT 2004


Dan Perl <dperl at rogers.com> wrote:

> I really like this one.  I think this 'defaultvalue' class even deserves to
> be added as a builtin.  It can even be changed to initialize non-empty
> sequences.  And I haven't tried that yet, but it should be able to
> initialize an attribute with any kind of object that can be constructed with
> no arguments.  Too bad it works only for instance attributes in a class.

As I posted on the other subthread, you can perfectly well use arguments
in the call, too -- just add *args and **kwds to the descriptor's
__init__ and use them when you later call the factory.  This is an
important patter in Python: whenever you design a callable X which
accepts a callable Y as an argument, consider adding *args and **kwds to
X's signature, stashing them away together with Y, and passing them back
into Y when the time comes to call it.  This can save users of X writing
lambdas or def functions which have no reason to exist except fixing
such parameters.


> 
> I will be a good Pythoneer/Pythonista and I will invoke parent __init__'s
> whenever using a library.  My concern was not as a library user, but as a
> library developer.  In which case I don't like the idea of relying on users
> to be good Python coders.

If they don't follow good, normal coding practices there is really
little you can do to save them from themselves.  Surely nothing worth
distorting all OO design to ensure every class can be meaningfully
initialized without parameters, as I explained in another part of this
thread.


Alex



More information about the Python-list mailing list