Hack Forums
[Python] How to inherit methods from an existing instance? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] How to inherit methods from an existing instance? (/showthread.php?tid=5968877)



How to inherit methods from an existing instance? - Meelo XO - May 11, 2019

I want to be able to use HTMLPrime with any other class.

Code
import cgi

class ClassX(object):
 pass # ... with own __repr__

class ClassY(object):
 pass # ... with own __repr__

inst_x=ClassX()

inst_y=ClassY()

inst_z=[ i*i for i in range(25) ]

inst_b=True

class HTMLPrime(object):
  def html(self): # an "enhanced" version of __repr__
      return cgi.escape(self.__repr__()).join(("<H1>","</H1>"))

print HTMLPrime(inst_x).html()
print HTMLPrime(inst_y).html()
wrapped_z = HTMLPrime(inst_z)
inst_z[0] += 70
wrapped_z[0] += 71
print wrapped_z.html()
print HTMLPrime(inst_b).html()

Here is the output:

Code
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    print HTMLPrime(inst_x).html()
TypeError: object() takes no parameters

What's wrong?