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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:12:28+00:00 2026-05-13T07:12:28+00:00

On Python 2.5 I need to use float numbers with a modified __str__() method.

  • 0

On Python 2.5 I need to use float numbers with a modified __str__() method. Also I need to know when the constructor fails.

Why I can’t catch exceptions raised from float.__init__()?

What is the best way to consult the numeric value of my derived float object? In my code I’m using float(self).

class My_Number(float):
    def __init__(self, float_string):
        try:
            super(My_Number, self).__init__(float_string)
        except (TypeError, ValueError):
            raise My_Error(float_string)

    def __str__(self):
        if int(float(self)) == float(self):
            return str(int(float(self)))
        else:
            return str(round(float(self), 2))


>>> n = My_Number('0.54353')
>>> print n
0.54

>>> n = My_Number('5.0')
>>> print n
5

>>> n = My_Number('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): foo
  • 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-13T07:12:29+00:00Added an answer on May 13, 2026 at 7:12 am

    float is immutable, therefore its __init__, the initializer, is basically a no-op — nothing substantial can happen there, because the self object cannot be altered (if it’s actually an instance of float rather than of a subclass — but of course float‘s own __init__ must operate on that assumption;-).

    Therefore, all the action happens in __new__, the constructor proper, just like for other immutable types like int, str, tuple, and so on. It’s a common mistake to believe that __init__ is a constructor: it’s not, it takes an already-constructed object as its first argument, self, and “initializes” it (if feasible, i.e., if that self is mutable!-) — the construction itself happens in __new__.

    So, your float subclass should start:

    class My_Number(float):
      def __new__(cls, float_string):
        try: return float.__new__(cls, float_string)
        except (TypeError, ValueError): raise My_Error(float_string)
    

    and you can remove the __init__, which is not needed. Now:

    >>> n = My_Number('foo')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 4, in __new__
    NameError: global name 'My_Error' is not defined
    

    (of course, it would work even better if you did have a My_Error exception class defined;-).

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

Sidebar

Related Questions

Let's say I need to use Python and C++. I can call Python function
Need to use PGP/GnuPG to encrypt.can suggest what the Python packages to use that.
I need to use python-rest-client package into my project. I tried several times for
I need to use an embedded system running Python 1.5.2+ (!!!) with very few
I am beginner in python, and I need to use some thirdparty function which
I need to migrate some C# code to Python. The original code makes use
I need a Python equivalent of PHP's include function. I know of execfile(), but
I need to keep track of units on float and int values in Python,
I need to use METAR weather information in a python script. I found http://pypi.python.org/pypi/metar/1.4.0
I read that I can use Python's Format Specification Mini-Language to have more control

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.