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 8013001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:30:28+00:00 2026-06-04T19:30:28+00:00

I have devised a proxy class that allows me to replace any type/class with

  • 0

I have devised a proxy class that allows me to replace any type/class with a factory method and still retain most of the functionality of the class object. Here is a sample of how it works:

class ProxyClass:
    def __init__(self, cls):
        self._ProxyClass_cls = cls
    def __getattr__(self, name):
        return getattr(self._ProxyClass_cls, name)

class _strProxy(ProxyClass):
    def __call__(self, s):
        if '\n' in s:
            raise ValueError
        if s not in self._cache:
            self._cache[s] = self._ProxyClass_cls(s)
        return self._cache[s]

str = _strProxy(str)
str._cache = {}


>>> s = str('hello')
>>> s
'hello'
>>> type(s)
<type 'str'>
>>> str('hello\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __call__
ValueError

I like this implementation of factory methods because it completely replaces the original class object and still allows things like the following:

>>> map(str.split, [str('foo bar'), str('bar foo')])
[['foo', 'bar'], ['bar', 'foo']]

The only problem I have found is with operations on the class itself such as repr():

>>> repr(str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor '__repr__' of 'str' object needs an argument

In this example, repr() is attempting to call str.__repr__() rather than type.__repr__(str). I tried fixing changing str.__class__ but found out that this is impossible in this case:

>>> str.__class__ = type
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ must be set to a class

Does anyone know of any way to restore the functionality of repr(str) or maybe another way to accomplish what I am doing?

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-06-04T19:30:30+00:00Added an answer on June 4, 2026 at 7:30 pm

    Make sure that all your classes inherit from object:

    class ProxyClass(object):
    

    In Python 3.x, all classes inherit from object anyway, so you don’t need to do this. But in Python 2.x, the inheritance stuff doesn’t work right unless all the classes involved are “new-style classes” that inherit from object.

    repr(str) works fine for me after making the above change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have devised the following method for catching errors throughout my AS3 applications: In
From my experience I have learn that using an surrogate INT data type column
I have a sample Rails 3.1.1 application that I have set devise up to
I have 2 models Users(devise gem) and Profiles where class User < ActiveRecord::Base has_one
I have a MYSQL table that is 1275 fields wide. Each row in the
I have a app-type web site with a couple of long list pages (such
I have a Rails app that is running on port 8080 that I need
I have a file of about 30000 lines of data that I want to
I have a process that runs in it's own thread and can be started/stopped
I have devise + a scaffold house created, and I want that a user

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.