Newbie Question: Abstract Class in Python

K

Kevin Bass

Hello:

I am new to Python and want to know how to implement an abstract class? I
have read through different books and I have no found this information.
Thanks!

Kevin
 
A

Aahz

I am new to Python and want to know how to implement an abstract
class? I have read through different books and I have no found this
information. Thanks!

Simple answer: you don't. Python doesn't really have that concept. If
you tell us what you're trying to do, we can explain how to do that in
Python.
 
E

Erik Max Francis

Kevin said:
I am new to Python and want to know how to implement an abstract
class? I
have read through different books and I have no found this
information.
Thanks!

There's no builtin way to do that in Python. Typically if I'm doing
something where I want to make a class that yells at me if I 1.
accidentally instantiate it directly or 2. fail to override one of its
methods in a subclass, I do something like:

class AbstractSomething:
def __init__(self):
if self.__class__ is AbstractSomething:
raise NotImplementedError

def aMethod(self, ...):
raise NotImplementedError

def anotherMethod(self, ...):
raise NotImplementedError
 
B

Ben Finney

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.

Python, in general, doesn't try to stop the programmer doing things, the
way many other languages do. This is known in the community as the
"we're all consenting adults" philosophy.

If you can tell us why you'd want to prevent class instantiation, we can
discuss that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Newbie 0
Math python question 10
Abstract class 22
Variable class name in python 0
OOP & Abstract Classes 8
[Question]JS methods inside class 1
module to implement Abstract Base Class 0
Abstract Classes 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top