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

The Archive Base Latest Questions

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

When subclassing a base type, like float, is it possible to recalculate or reassign

  • 0

When subclassing a base type, like float, is it possible to “recalculate” or “reassign” the original value? If I have the following class definition,

import collections

class MovingAverage(float):
    def __new__(self, initial_value, window):
        self.d = collections.deque([initial_value], window)
        return float.__new__(self, initial_value)
    def add(self, new_value):
        self.d.append(new_value)
        print sum(self.d) / len(self.d)
        # here, I want to _set_ the value of MovingAverage to
        #     sum(self.d) / len(self.d)

When I start with

>>> ma = MovingAverage(10, 3)
>>> ma
10.0

but

>>> ma.add(3)
6.5
>>> ma
10.0

The other class definition I’ve tried is this:

import collections

class MovingAverage(float):
    def __new__(self, iterable, window):
        self.d = collections.deque(iterable, window)
        initial_value = sum(iterable) / len(iterable)
        return float.__new__(self, initial_value)
    def add(self, new_value):
        self.d.append(new_value)
        return MovingAverage(self.d, self.d.maxlen)

This time, when I start with

>>> ma = MovingAverage([10], 3)
>>> ma
10.0

and

>>> ma.add(3)
6.5
>>> ma
10.0
>>> ma = ma.add(3)
>>> ma
5.333333333333333

However, I think (I haven’t tested to find out) it makes this significantly slower. So, can it be done? Can I somehow set it so that the return from ma is the value that I’m looking for? Or do I need to define a value attribute, change the base class to object, and abandon my pretense that I have a chance of controlling the return value of the class?

  • 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-23T16:32:24+00:00Added an answer on May 23, 2026 at 4:32 pm

    No. Since these types are immutable, you should be using encapsulation, not inheritance.

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

Sidebar

Related Questions

Lets say I have the following code: public class Base { // Some stuff
In following code, I want to extend the behaviour of a class by deriving/subclassing
If one can prevent subclassing by declaring private constructor in the base class, why
I would like to use a base message class like: [Serializable] public abstract class
Is it possible to chain metaclasses? I have class Model which uses __metaclass__=ModelBase to
I have an abstract base class that I'm referencing from one of my entities.
I have been reading the MSDN documentation on subclassing and I have been successful
I've got a base form in Delphi 2007 that I'd like to reuse in
As mentioned in the Zend Framework manual , I created a base controller. Subclassing
I've got a base class where I want to handle __add__() and want 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.