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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:05:52+00:00 2026-05-15T14:05:52+00:00

I’m not clear how to pose this question. If I did, I’d probably be

  • 0

I’m not clear how to pose this question. If I did, I’d probably be a lot closer to a solution.. I need some insight into inheritance.

I want to make a custom subtype of float. But I want the instance of this subtype to re-evaluate its value before performing any of the normal float methods (__add__,__mul__, etc..). In this example it should multiply its value by the global FACTOR:

class FactorFloat(float):
    # I don't think I want to do this:
    ##    def __new__(self, value):
    ##        return float.__new__(self, value)
    def __init__(self, value):
        float.__init__(self, value)
    # Something important is missing..
    # I want to do something with global FACTOR
    # when any float method is called.

f = FactorFloat(3.)
FACTOR = 10.
print f   # 30.0
print f-1 # 29.0
FACTOR = 2.
print f   # 6.0
print f-1 # 5.0

This is a just a sanitized example that I think gets my point across. I’ll post a more complex “real” problem if necessary.

  • 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-15T14:05:52+00:00Added an answer on May 15, 2026 at 2:05 pm
    class FactorFloat(float):
        def _factor_scale(f):
            def wrapper(self, *args, **kwargs):
                scaled = float.__mul__(self, FACTOR)
                result = f(scaled, *args, **kwargs)
                # if you want to return FactorFloats when possible:
                if isinstance(result, float):
                    result = type(self)(result/FACTOR)
                return result
            return wrapper
    
        def __repr__(self):
            return '%s(%s)' % (type(self).__name__, float.__repr__(self))
    
        __str__ = _factor_scale(float.__str__)
        __mul__ = _factor_scale(float.__mul__)
        __div__ = _factor_scale(float.__div__)
        __add__ = _factor_scale(float.__add__)
        __sub__ = _factor_scale(float.__sub__)
    
    
    f = FactorFloat(3.)
    FACTOR = 10.
    print f   # 30.0
    print f-1 # 29.0
    FACTOR = 2.
    print f   # 6.0
    print f-1 # 5.0
    print repr(f)
    

    for:

    30.0
    29.0
    6.0
    5.0
    FactorFloat(3.0)
    

    EDIT:

    In response to the question in the comment; making things slightly more general and automated, using a class decorator. I would not loop over dir(baseclass), but instead would explicitly list the methods I wished to wrap. In the example below, I list them in the class variable _scale_methods.

    def wrap_scale_methods(cls):
        Base = cls.__base__
        def factor_scale(f):
            def wrapper(self, *args, **kwargs):
                scaled = Base.__mul__(self, FACTOR)
                result = f(scaled, *args, **kwargs)
                if isinstance(result, Base):
                    result = type(self)(result/FACTOR)
                return result
            return wrapper
        for methodname in cls._scale_methods:
            setattr(cls, methodname, factor_scale(getattr(Base, methodname)))
        return cls
    
    @wrap_scale_methods
    class FactorComplex(complex):
        _scale_methods = '__str__ __mul__ __div__ __add__ __sub__'.split()
        def __repr__(self):
            return '%s(%s)' % (type(self).__name__, complex.__repr__(self)[1:-1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
I'm looking for suggestions for debugging... If you view this site in Firefox or
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.