[Python-Dev] Replacing __slots__ with addmembers()

Michael McLay mclay@erols.com
Mon, 12 Nov 2001 11:14:02 -0500


Barry Warsaw writes:
> Without BDFL override, yes, it does.
>
> >>>>> "TP" == Tim Peters <tim.one@home.com> writes:
>
>    TP> I expect we will.  Ditto __metatype__, long-winded super(),
>    TP> function-based property "declarations", and all the other new
>    TP> stuff.  We're aiming for progress with the new features, not
>    TP> perfection <wink>.
>
> I believe Guido knows that it will be impossible to get all this stuff
> right the first time, and even the 2.2 beta cycle won't shake out all
> the problems.  I think his intention was to get the basic
> functionality in place for Python 2.2, and to clean up and improve the
> syntax and semantics in future releases.

I hope Python Labs doesn't rush Python 2.2 out early just to meet an
artificial schedule.  MAL's concerns about cluttering Python with
several 
variations for spelling the same kind of feature is shared by me. I'm
sure other long time users of Python would rather wait a few weeks 
to get something with fewer new warts.

Not all of these changes have the same level of impact on day to day
programming. The __metatype__ and super() feature will be used far 
less than __slots__. Spelling __slots__ in a more pythonic manner now
will eliminate having lots of ugly code being supported forever.

Tim suggested that the type checking feature might be enough to get the 
patch rejected.  For the moment let's assume I had not added that
feature.
The patch does still makes adding member_descriptors more consistent
with
the syntax used to add properties. There is consistency in usage with

  property(fget, fset, doc)
  addmember(default, doc)

Adding the default capability to adding a member is important because it
will eliminate a very common initialization pattern.  Instead of
writing:

class B(object):
  __slots__ = ['a','b']
  def __init__(self,a=1,b="cats and dogs"):
      self.a=a
      self.b=b

The addmember() feature reduces this to an easy to read:

class B(object):
  a = addmember(default=1, doc="the ever popular a attribute")
  b = addmember(default="cats and dogs", doc="the b docstring")

This also allows docstrings to be added to the attributes, a feature
that isn't possible with the current __slots__ spelling.

The type checking feature can easily be removed.  I hope this doesn't
happen.  This feature replaces another very common design pattern. While
dynamic typing is very powerful, there are many occasions when data is 
required to be of a specific type.  For instance, engineering data 
exchange standards are filled with type checking requirements.
Spacecraft
crash into Mars for a lack of good checks.  This is not only
embarrassing,
it's expensive.
 
Examples of type checking in Python are described in:
 - http://mail.python.org/pipermail/python-dev/2001-October/017852.html

The approach to adding this capability using addmember() is very 
pythonic. It is completely unintrusive to anyone who doesn't need it
and it is very easy to use for those occasions when you must use it.