Question on ABC classes

Ethan Furman ethan at stoneleaf.us
Thu Oct 22 17:03:41 EDT 2020


On 10/22/20 9:25 AM, Julio Di Egidio wrote:
> Now, I do read in the docs that that is as intended,
> but I am not understanding the rationale of it: why
> only if there are abstract methods defined in an ABC
> class is instantiation disallowed? IOW, why isn't
> subclassing from ABC enough?

Let's say you subclass from ABC:
 class Abstract(ABC):
 pass
Then you subclass from that:
 class Concrete(Abstract):
 pass
Then subclass from that:
 class MoreConcrete(Concrete):
 pass
If you do a
 issubclass(<any of the above classes>, ABC)
you'll get
 True
The idea behind abstract classes is the prevention of creating non-functional instances, which means if any abstract 
methods, properties, etc., are present in an abstract class, then it's instances will not be fully functional; 
contrariwise, if there are no abstract anythings in the class, then it is functional and there's no reason not to allow 
it to be created.
Put another way: if ABC is anywhere in a class' parentage, then it is "abstract" -- the only way to tell if 
instantiating it is okay is by the presence/absence of abstract pieces in the class.
--
~Ethan~


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /