What do you call a class not intended to be instantiated
Roy Smith
roy at panix.com
Sat Sep 27 10:20:50 EDT 2008
In article <gblea5$os6$1 at panix3.panix.com>, aahz at pythoncraft.com (Aahz)
wrote:
> One cute reason to prefer class singletons to module singletons is that
> subclassing works well for creating multiple singletons. But really,
> the main reason I use class singletons is that they are the absolute
> simplest way to get attribute access:
>
> class Foo: pass
> Foo.bar = 'xyz'
> if data == Foo.bar:
> print "!"
I've often done something similar, creating a dummy class:
class Data:
pass
just so I could create instances of it to hang attributes off of:
foo = Data()
foo.bar = 'xyz'
That's a trick I've been using since the Old Days (i.e. before new-style
classes came along). When I saw your example, my first thought was "That's
silly, now that there's new-style classes, you can just create an instance
of object!". Unfortunately, when I tried it, I discovered it didn't work.
You *can* instantiate object, but you don't get a class instance, so you
can't create attributes on it.
>>> x = object()
>>> type(x)
<type 'object'>
>>> x.foo = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'foo'
More information about the Python-list
mailing list