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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:52:45+00:00 2026-06-05T12:52:45+00:00

I would like a class whose instances return a value when called directly as

  • 0

I would like a class whose instances return a value when called directly as if they inherit from int, float, str, etc. I imagine it’s overloading some function ???:

class A(object):
    def __init__(self, value):
        self.value = value

    def somefunction(self):
        if isintance(self.value, int):
            return 5
        else:
            return 'something else'

    def __???__(self):
        return self.value

a = A(2)
a + 5 # 7
a.somefunction() # 5

a = A('foo')
a + "bar" # 'foobar'
a.somefunction() # 'something else'

I can’t simply subclass int as the value could be of different types.

Is this doable? Perhaps there’s a good reason that this can’t be done, but it’s late, and I can’t think of it. Thanks in advance.

  • 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-05T12:52:46+00:00Added an answer on June 5, 2026 at 12:52 pm

    Overriding __getattr__ won’t work because of metaclass confusion; the issue is that the relevant methods need to be in the instance’s class dict.

    If you’re happy with value being immutable, one way could be to override __new__ and construct classes on demand:

    def __new__(cls, value):
        return type("A", (type(value), A), {})(value)
    

    Note that this puts A after type(value) in the mro; this is necessary to stop __new__ resulting in runaway recursion! If there’s methods on A that should override those on type(value), you can put them into the dict 3rd argument to type().

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

Sidebar

Related Questions

I would like to create a class whose methods can be called from multiple
I want to create a generic class, whose builder would not return an instance
I would like a class analogous to spring's ContextLoader/ContextLoaderListener/ContextLoadServlet. These classes are invoked when
I would like a has_many association that works like so: class Hood acts_as_tree has_many
I would like to access class variables with for loop, here is my simple
In c#, I would like the abstract class to be applied to many other
In Java, a 'static method' would look like this: class MyUtils { . .
I would like to make a class where the client side (proxy) of the
I would like to understand what class << self stands for in the next
I would like to create a class that behaves as a python variable but

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.