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

  • Home
  • SEARCH
  • 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 358891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:20:32+00:00 2026-05-12T12:20:32+00:00

I am trying to derive a class from a python primitive, the float, for

  • 0

I am trying to derive a class from a python primitive, the float, for the purpose of printing a different repr string when it’s printed out.

How do I access the underlying data from the derived class when I do this?

Here’s a simplified example of what I am trying to do:

class efloat(float):
    def __repr__(self):
        return "here's my number: %s" % str(WHAT CAN I PUT HERE???)

Ok, thanks folks! I think I get it now. Here’s the finished class for anyone who’s curious:

import math

class efloat(float):
    """efloat(x) -> floating point number with engineering representation when printed
       Convert a string or a number to a floating point number, if possible.
       When asked to render itself for printing (via str() or print) it is normalized
       to engineering style notation at powers of 10 in multiples of 3
       (for micro, milli, kilo, mega, giga, etc.)        
    """

    def _exponent(self):   
        if self == 0.0:
           ret = 0
        else:
           ret = math.floor(math.log10(abs(self)))
        return ret

    def _mantissa(self):
        return self/math.pow(10, self._exponent())

    def _asEng(self):
        shift = self._exponent() % 3

        retval = "%3.12ge%+d" % (self._mantissa()*math.pow(10, shift), self._exponent() - shift)
        return retval

    def __str__(self):
        return self._asEng()

    def __repr__(self):
        return str(self)

    def __add__(self, x):
        return efloat(float.__add__(self, float(x)))

    def __radd__(self, x):
        return efloat(float.__add__(self, float(x)))

    def __mul__(self, x):
        return efloat(float.__mul__(self, float(x)))

    def __rmul__(self, x):
        return efloat(float.__mul__(self, float(x)))

    def __sub__(self, x):
        return efloat(float.__sub__(self, float(x)))

    def __rsub__(self, x):
        return efloat(float.__rsub__(self, float(x)))

    def __div__(self, x):
        return efloat(float.__div__(self, float(x)))

    def __rdiv__(self, x):
        return efloat(float.__rdiv__(self, float(x)))

    def __truediv__(self, x):
        return efloat(float.__truediv__(self, float(x)))

    def __rtruediv__(self, x):
        return efloat(float.__rtruediv__(self, float(x)))

    def __pow__(self, x):
        return efloat(float.__pow__(self, float(x)))

    def __rpow__(self, x):
        return efloat(float.__rpow__(self, float(x)))

    def __divmod__(self, x):
        return efloat(float.__divmod__(self, float(x)))

    def __neg__(self):
        return efloat(float.__neg__(self))

    def __floordiv__(self, x):
        return efloat(float.__floordiv__(self, float(x)))
  • 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-12T12:20:33+00:00Added an answer on May 12, 2026 at 12:20 pm

    If you don’t override __str__, that will still access the underlying method, so:

    class efloat(float):
        def __repr__(self):
            return "here's my number: %s" % self
    

    will work. More generally, you could use self+0, self*1, or any other identity manipulation that you did not explicitly override; if you overrode them all, worst case, float.__add__(self, 0) or the like.

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

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

EDIT: minor fixes (virtual Print; return mpInstance) following remarks in the answers. I am
Coming from a long history of C-style syntax and now trying to learn Ruby
I am using a hierarchy of generic collection classes that derive from an abstract
I am trying to inherit a non-static class by a static class. public class

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.