Python Forum
problem with print command in super()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with print command in super()
#1
hi
in the below code:
'''
Python Multiple Inheritance and MRO
super usage() in python
from: https://www.geeksforgeeks.org/python-super/
'''
class A:
	def age(self):
		print("Age is 21")
class B:
	def age(self):
		print("Age is 23")
class C(A, B):
	def age(self):
		super(C, self).age()
	
c = C()
print(f"result of c.age()is {c.age()}")
when the print line is inactive, nothing is displayed. when the print line is active, the output will be(I use Idle):
Output:
Age is 21 result of c.age()is None
I expect the output will be: result of c.age is Age is 21
why the Age is 21 is displayed before the print result? and what is the None in the result of print? namely, is c.age=None?
thanks
Reply
#2
Method age() should not print anything, it should return a number! In your code, it returns None.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
hi
no explanation to the above question?

if I want to have in output: age is 23, namely in class C,replace of A, B is called, how can be changed line 14?
Reply
#4
I thought the explanation was clear. age() in all the classes needs to return a number. Currently it returns None. age() should not do a print.
class A:
    def age(self):
       return 21
class B:
    def age(self):
       return 23
class C(A, B):
    def age(self):
        return super(C, self).age()
     
c = C()
print(f"result of c.age()is {c.age()}")
But you really shouldn't specify the class for super. You should set up the class inheritance such that the MRO calls the correct superclass method. The only time you should use super(class, self) is when you need to go against the MRO.
class A:
    def age(self):
        return 21


class B:
    def age(self):
        return 23


class C(A, B):
    def age(self):
        return super().age()


c = C()
print(f"result of c.age()is {c.age()}")
I know this is just example code meant to demonstrate multiple inheritance and I shouldn't read much into the design, but it is a silly example and makes me question the quality of information you are getting from the link you reference.
akbarza likes this post
Reply
#5
hi, thanks for reply
in your code, without changing line 11 (class C(A, B)), how can I change line 13 ( return super().age() ) so that 23 is printed in output?
Reply
#6
You should change line 11, but you could call B.age(self) in line 13
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 919 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  Problems with super() Hoespilaar 2 271 Apr-10-2024, 10:10 AM
Last Post: Hoespilaar
  problem with spliting line in print akbarza 3 406 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  super() in class akbarza 1 474 Dec-19-2023, 12:55 PM
Last Post: menator01
  problem with print lists MarekGwozdz 4 697 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  problem in using input command akbarza 4 1,151 Oct-19-2023, 03:27 PM
Last Post: popejose
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,107 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Problem with print variable in print.cell (fpdf) muconi 0 670 Dec-25-2022, 02:24 PM
Last Post: muconi
  Facing Problem while opening a file through command prompt vlearner 4 1,922 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Why does absence of print command outputs quotes in function? Mark17 2 1,394 Jan-04-2022, 07:08 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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