Python Forum
Behavior of Abstract Base Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Behavior of Abstract Base Class
#1
All,

Is this behavior correct for ABC class? I expected only the method in class S to be printed. Is this the correct behavior?

import abc 
from abc import ABC, abstractmethod 

class R(ABC): 
	def rk(self): 
		print("Abstract Base Class") 

class K(R): 
	def rk(self): 
		super().rk() 
		print("subclass ") 
        
class S(K):
    def rk(self):
        super().rk()
        print("Class S")

# Driver code 
 
t = S()
t.rk()

'''
This prints

Abstract Base Class
subclass
Class S

'''
All the Best,
David
Reply
#2
You are not using ABC at all. If you want to see why someone thought Python needed abstract base classes read the PEP https://www.python.org/dev/peps/pep-3119/

For your example it makes no difference what is the base class of R.
class CBC():
    pass
 
class R(CBC): 
    def rk(self): 
        print("R") 
 
class K(R): 
    def rk(self): 
        super().rk() 
        print("K") 
         
class S(K):
    def rk(self):
        super().rk()
        print("S")
 
# Driver code 
  
t = S()
t.rk()
Output:
R K S
Why did you not expect S.rk() to call K.rk() when it the method calls super().rk()? What did you expect to happen? What problem are you trying to solve that made you look at using abstract base classes?
dgrunwal likes this post
Reply
#3
Thank you. I read the PEP https://www.python.org/dev/peps/pep-3119/ and it answered my question. I was not seeing the super() clear enough.

I don't have a particular use case for using abstract base classes. One reason for ABC e.g., someone who wants to define a generic function (PEP 3124) for any sequence that has an append() method. Python is very extensible. Worth the time. Thanks.

Best,
Dave
Reply
#4
I did learn new stuff about usage and what Abstract Base Classes are all about lookin at:
Raymond Hettinger «Build powerful, new data structures with Python's abstract base classes»
Gribouillis and buran like this post
Reply
#5
@snippsat It is really a very good talk. He has a deep understanding of the software.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a base class variable from an inherited class CompleteNewb 3 1,673 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Importing issues with base class for inheritance riccardoob 5 4,665 May-19-2021, 05:18 PM
Last Post: snippsat
  Abstract classes SephMon 8 3,110 Aug-30-2020, 05:03 AM
Last Post: Gribouillis
  How to convert TransactSQL into Abstract Syntax Tree (AST) using Python? bajrangbs 2 2,302 Jan-30-2020, 03:46 PM
Last Post: micseydel
  how can I changing a base class method voidptr 2 2,254 Nov-10-2019, 10:53 PM
Last Post: voidptr
  Polymorphism not working with a call to a abstract method colt 3 2,316 Nov-04-2019, 11:04 PM
Last Post: colt
  How to call base class function kamal_chennai 1 3,331 Apr-03-2019, 01:12 PM
Last Post: ichabod801
  Why Can't I call base class function for my derived class object? AmirAB 2 2,560 Feb-13-2019, 03:04 PM
Last Post: snippsat
  Base class variables are not accessible Prabakaran141 3 2,808 Oct-31-2018, 07:13 AM
Last Post: buran
  The derivate class dosn't behave like the base when i pass parameter to them drudox 4 3,159 Aug-05-2018, 06:42 PM
Last Post: drudox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020