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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:24:43+00:00 2026-05-23T23:24:43+00:00

I recently developed a class named DocumentWrapper around some ORM document object in Python

  • 0

I recently developed a class named DocumentWrapper around some ORM document object in Python to transparently add some features to it without changing its interface in any way.

I just have one issue with this. Let’s say I have some User object wrapped in it. Calling isinstance(some_var, User) will return False because some_var indeed is an instance of DocumentWrapper.

Is there any way to fake the type of an object in Python to have the same call return True?

  • 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-05-23T23:24:44+00:00Added an answer on May 23, 2026 at 11:24 pm

    Testing the type of an object is usually an antipattern in python. In some cases it makes sense to test the “duck type” of the object, something like:

    hasattr(some_var, "username")
    

    But even that’s undesirable, for instance there are reasons why that expression might return false, even though a wrapper uses some magic with __getattribute__ to correctly proxy the attribute.

    It’s usually preferred to allow variables only take a single abstract type, and possibly None. Different behaviours based on different inputs should be achieved by passing the optionally typed data in different variables. You want to do something like this:

    def dosomething(some_user=None, some_otherthing=None):
        if some_user is not None:
            #do the "User" type action
        elif some_otherthing is not None:
            #etc...
        else:
             raise ValueError("not enough arguments")
    

    Of course, this all assumes you have some level of control of the code that is doing the type checking. Suppose it isn’t. for “isinstance()” to return true, the class must appear in the instance’s bases, or the class must have an __instancecheck__. Since you don’t control either of those things for the class, you have to resort to some shenanigans on the instance. Do something like this:

    def wrap_user(instance):
        class wrapped_user(type(instance)):
            __metaclass__ = type
            def __init__(self):
                pass
            def __getattribute__(self, attr):
                self_dict = object.__getattribute__(type(self), '__dict__')
                if attr in self_dict:
                    return self_dict[attr]
                return getattr(instance, attr)
            def extra_feature(self, foo):
                return instance.username + foo # or whatever
        return wrapped_user()
    

    What we’re doing is creating a new class dynamically at the time we need to wrap the instance, and actually inherit from the wrapped object’s __class__. We also go to the extra trouble of overriding the __metaclass__, in case the original had some extra behaviors we don’t actually want to encounter (like looking for a database table with a certain class name). A nice convenience of this style is that we never have to create any instance attributes on the wrapper class, there is no self.wrapped_object, since that value is present at class creation time.

    Edit: As pointed out in comments, the above only works for some simple types, if you need to proxy more elaborate attributes on the target object, (say, methods), then see the following answer: Python – Faking Type Continued

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

Sidebar

Related Questions

All, Recently I developed a code that supposedly is a thread-safe class. Now the
In our application we are caching some frequently used data. Recently we have developed
I got an old Program(class library) developed by .Net 2 . Recently I want
I recently began using BIRT and have developed a report to use with my
I've been a web developer for some time now, and have recently started learning
I recently developed an interop user control in .NET (Visual Studio 2008, project targetting
I have recently developed an Eclipse plugin, however the plugin does not seem to
I got to know about this Java 1.5 capability recently and I developed a
I have recently developed a habit of running all of my programs through valgrind
I took over an iPhone project recently that was developed prior iOS4. I'm wondering

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.