Newbie Question: Abstract Class in Python

Michele Simionato mis6 at
Tue Jul 8 00:35:18 CEST 2003

"Kevin Bass" <kbass1 at> wrote in message news:<Tl3Oa.239706$jp.6488300 at>...
> 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-
  File "<pyshell#5>", line 3, in __new__
    raise TypeError("Non-instantiable class")
TypeError: Non-instantiable class



More information about the Python-list mailing list