PEP 252: __slots__ inappropriate

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Aug 16 03:16:28 EDT 2001


In the current implementation of PEP 252, to declare an class with
slots, you assign to the __slots__ variable inside the class
definition:

class Foo(object):
  __slots__ = ['bar','baz']

This declares that every instance of Foo will have two instance
attributes, namely bar and baz.

I find that this way of defining class properties is inconsistent with
the rest of the language, in particular with other "magic" class
attributes:

__bases__: Not defined by assigning to __bases__, but by putting
           them after the class name in parentheses
__doc__: Not defined by assigning to __doc__, but by putting a 
         string as the first thing inside Foo
__dict__: Not defined by assigning to __dict__, but filled implicitly
__name__: Given after the class keyword

And so on. So I would like to request that proper syntax is added,
where the slots names are syntactically identifiers instead of being
strings. Perhaps it is possible to combine this with a default value
for the slots: currently, each slot has a default of None.

There is a number of options to improve the syntax for declaring
slots, such as

A. A new keyword

class Foo:
  slot bar
  slot baz = 42

This is similar in declaration to a global directive, except that that
it would be a definition, not a declaration: It would create a member
object.

B. An existing keyword

class Foo:
  def bar
  def baz = 42

C. A new builtin

class Foo:
  bar = slot()
  baz = slot(42)

In this option, the slot object would not know what its name is. So
this would require some magic, either at class definition time, or at
slot access time.

So please consider changing the slot syntax.

Regards,
Martin




More information about the Python-list mailing list