Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 32481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:46:11+00:00 2026-05-10T13:46:11+00:00

Below I have a very simple example of what I’m trying to do. I

  • 0

Below I have a very simple example of what I’m trying to do. I want to be able to use HTMLDecorator with any other class. Ignore the fact it’s called decorator, it’s just a name.

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 HTMLDecorator(object):    def html(self): # an 'enhanced' version of __repr__        return cgi.escape(self.__repr__()).join(('<H1>','</H1>'))  print HTMLDecorator(inst_x).html() print HTMLDecorator(inst_y).html() wrapped_z = HTMLDecorator(inst_z) inst_z[0] += 70 wrapped_z[0] += 71 print wrapped_z.html() print HTMLDecorator(inst_b).html() 

Output:

Traceback (most recent call last):   File 'html.py', line 21, in      print HTMLDecorator(inst_x).html() TypeError: default __new__ takes no parameters

Is what I’m trying to do possible? If so, what am I doing wrong?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:46:12+00:00Added an answer on May 10, 2026 at 1:46 pm

    Very close, but then I lose everything from ClassX. Below is something a collegue gave me that does do the trick, but it’s hideous. There has to be a better way.

    Looks like you’re trying to set up some sort of proxy object scheme. That’s doable, and there are better solutions than your colleague’s, but first consider whether it would be easier to just patch in some extra methods. This won’t work for built-in classes like bool, but it will for your user-defined classes:

    def HTMLDecorator (obj):     def html ():         sep = cgi.escape (repr (obj))         return sep.join (('<H1>', '</H1>'))     obj.html = html     return obj 

    And here is the proxy version:

    class HTMLDecorator(object):     def __init__ (self, wrapped):         self.__wrapped = wrapped      def html (self):         sep = cgi.escape (repr (self.__wrapped))         return sep.join (('<H1>', '</H1>'))      def __getattr__ (self, name):         return getattr (self.__wrapped, name)      def __setattr__ (self, name, value):         if not name.startswith ('_HTMLDecorator__'):             setattr (self.__wrapped, name, value)             return         super (HTMLDecorator, self).__setattr__ (name, value)      def __delattr__ (self, name):         delattr (self.__wraped, name) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've got a very simple CSS example detailed below. The .entry class generates a
I have a very simple two column setup shows below. However in example one
I have a very simple Winsock2 TCP client - full listing below - which
I have written a very simple WCF service, that worked fine (code below), then
The below code is very simple. I have a jQuery autocomplete bound to an
I have created a very simple Entity Framework 4.0 model using VS2010 Professional. Ignore
I followed a simple example as below. But compile failed. Could you please have
I am trying to learn JavaScript, but I'm struggling with a very simple example.
I have a java threads related question. To take a very simple example, lets
I have a very simple application where I'm trying to bind keyboard shortcuts to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.