mixin helper class for unknown attribute access?

Sam Pointon free.condiments at gmail.com
Mon Oct 31 14:12:11 CET 2005


One alrady exists, __slots__.

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


>>> f = Foo()
>>> f.foo = 'bar'

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in -toplevel-
    f.foo = 'bar'
AttributeError: 'Foo' object has no attribute 'foo'
>>> f.bar = 'foo'

However, __slots__ only works for class instances, so if you're messing
around with uninitialised classes (not a good idea outside the
singleton pattern or very functional-style code) it won't work.




More information about the Python-list mailing list