Newbie Question: Abstract Class in Python

Michele Simionato mis6 at pitt.edu
Mon Jul 7 18:35:18 EDT 2003


"Kevin Bass" <kbass1 at nc.rr.com> wrote in message news:<Tl3Oa.239706$jp.6488300 at twister.southeast.rr.com>...
> I am only looking for an answer about creating an abstract class in Python
> and
> not a solution to a problem.   I am looking for a way to stop class
> instantiation
> of a class. This should be a generic approach.
> 
> Kevin

I may stop instantiation by inheriting from

>>> class NonInstantiable(object):
	def __new__(cls,*args,**kw):
		raise TypeError("Non-instantiable class")

Here is how it works:
	
>>> class C(NonInstantiable):pass

>>> c=C()

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in -toplevel-
    c=C()
  File "<pyshell#5>", line 3, in __new__
    raise TypeError("Non-instantiable class")
TypeError: Non-instantiable class

HTH,

          Michele




More information about the Python-list mailing list