Solution: Direct access to Printer I/O lines

Alex Martelli aleaxit at
Fri Dec 15 07:16:13 EST 2000

<jkndeja at> wrote in message news:91cnp3$g59$1 at
> I have the following thoughts about the interface, and would be
> interested in any comments:


> - The module implements a class, say directlpt
> - The module only allows three 'Singleton' instances of the class,
> corresponding to LPT1--3. This is set up in the constructor:
> import directlpt
> l1 = directlpt.directlpt(1) # gives access to LPT1:
> anotherl1 = directlpt.directlpt(1) # will raise an exception - only one
> instance allowed

This gives me a good chance for one of my favourite rants,
'"Singleton" design-pattern considered harmful'!-)  But
'Singleton' is not being ideally applied, so I'll skip that.

Why given an error when the object mapping the same LPTx: is
asked-for twice?  Just return what is, functionally, "the same"
object -- whether the id() is the same or not is quite a
secondary issue, so, if you want to keep directlpt as a
class, that's OK too -- the id() will differ [you can't
change what's returned by "calling the class", so a new
object will come up each time], but that's OK as long as
'different' object for the same printer-port share all
state among them.  Or else, make directlpt.directlpt into
a factory function, returning an object of class (or type)
'_directlpt', say, in which case you may be able to also
get the same identities.

Either of these will work better than constraining different
client-code modules to coordinate their generation and
release of directlpt objects.  I think the pattern based
on different-identities/same-state has advantages; for
example, it makes it possible (though it may still be
tricky) for client-code to _inherit_ from directlpt:

class PrettyLpt(directlpt):
    def __init__(self, n, doodad):
        directlpt.__init__(self, n)
        self.doodad = doodad
    # etc, etc

If you do want to allow that, more or less unrestrainedly,
you'll have to take some care to avoid name clashes (but
starting directlpt's attribute names with '__' will do:-).


More information about the Python-list mailing list