how to make super() work with externally defined methods in inheritance???

Paint-It-Black thomas.lynch at reasoningtechnology.com
Wed Aug 15 17:40:21 EDT 2018


> ChrisA

Yes, that works.  Thank you all for your help.  -->


class A:
  def __init__(self):
    self.number = 1

def A_biginc(self):
  self.number += 107

A.biginc = A_biginc

class B(A):
  def __init__(self):
    super().__init__()
    print("making a B")

def B_biginc(self):
  super(B,self).biginc() # super() trips up
  super(B,self).biginc()
  return self.number

B.biginc = B_biginc

def f_ut_oh():
   a = A()
   flag1 = a.number == 1
   a.biginc()
   flag2 = a.number == 108

   b = B()
   flag3 = b.number == 1
   b.biginc() 
   flag4 = b.number == 215

   if flag1 and flag2 and flag3 and flag4 :
     print("all good!") 
   else:
     print("hmm something went wrong..")

print("loaded try_adding_method.py")

---->

Python 3.6.6 (default, Jun 27 2018, 14:44:17) 
[GCC 8.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> python.el: native completion setup loaded
>>> loaded try_adding_method.py
>>> f_ut_oh()
making a B
all good!
>>> 



More information about the Python-list mailing list