mixin helper class for unknown attribute access?
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Mon Oct 31 09:04:15 EST 2005
On Mon, 31 Oct 2005 05:12:11 -0800, Sam Pointon wrote:
> 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'
__slots__ are NOT intended to be used to limit Python's dynamic nature.
Whether you call this usage a misuse or a serendipitous side-effect is a
matter of opinion.
> 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.
__slots__ only work with new-style classes, not classic classes.
Before using __slots__, read this:
http://www.python.org/doc/current/ref/slots.html
Then read this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252158
--
Steven.
More information about the Python-list
mailing list