What do you call a class not intended to be instantiated
James Mills
prologic at shortcircuit.net.au
Sun Sep 21 18:54:43 EDT 2008
Hi,
Wouldn't a normal class called State
suffice for storing state between calls ?
And ... Creating a state instance ?
For example:
class State(object):
"""State() -> new state object
Creates a new state object that is suitable
for holding different states of an application.
Usefull in state-machines.
The way this works is rather simple. You create a new
state object, and simply set the state. If the state
doesn't exist, it's added to it's internal data
structure. The reason this is done is so that
comparing states is consistent, and you can't just
compare with a non-existent state.
"""
def __init__(self):
"initializes x; see x.__class__.__doc__ for signature"
self._states = {}
self._next = 0
# Default States
self._add("START")
self._add("DONE")
def __repr__(self):
try:
return "<State: %s>" % self._state
except AttributeError:
return "<State: ???>"
def __str__(self):
return self._state
def __eq__(self, s):
return s in self._states and self._state == s
def __lt__(self, s):
return s in self._states and self._state == s and \
self._states[s] < self._states[self._state]
def __gr__(self, s):
return s in self._states and self._state == s and \
self._states[s] > self._states[self._state]
def _add(self, s):
self._states[s] = self._next
self._next = self._next + 1
def set(self, s):
"""S.set(s) -> None
Set the current state to the specified state given by s,
adding it if it doesn't exist.
"""
if s not in self._states:
self._add(s)
self._state = s
cheers
James
On Mon, Sep 22, 2008 at 8:39 AM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> I have a class which is not intended to be instantiated. Instead of using
> the class to creating an instance and then operate on it, I use the class
> directly, with classmethods. Essentially, the class is used as a function
> that keeps state from one call to the next.
>
> The problem is that I don't know what to call such a thing! "Abstract
> class" isn't right, because that implies that you should subclass the
> class and then instantiate the subclasses.
>
> What do you call such a class?
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
--
-- "Problems are solved by method"
More information about the Python-list
mailing list