Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

How to inherit methods from an existing instance?

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?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by HowmygoadDec 29, 2021 11:22 AM 3 221 Dec 29, 2021 11:22 AM
Last Post: Howmygoad
[C] by minerwOct 7, 2020 05:44 AM 1 116 Oct 7, 2020 05:44 AM
Last Post: hell-o-world
[C#] by itcorpJan 16, 2019 03:46 PM 7 340 Jan 16, 2019 03:46 PM
Last Post: itcorp