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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:54:39+00:00 2026-06-12T10:54:39+00:00

I need a class that is an integer whose value can be changed after

  • 0

I need a class that is an integer whose value can be changed after the object is created.
I need this class to define a size that is first specified in millimeters. Later when the user-interface is created, I get a factor from the device-context that converts millimeters to pixels. This factor should change the millimeter-value of my object to a pixel-value.

I tried to subclass int (see increment int object), but int is immutable so I can’t change its value.

class UiSize(int):
    def __new__(cls, value=0):
        i = int.__new__(cls, value)
        i._orig_value = value
        return i

    def set_px_per_mm(self, px_per_mm):
        pixel_value = int(round(self._orig_value * px_per_mm))
        print "pixel_value", pixel_value
        # how to set the new pixel_value to the object's value ?

s = UiSize(500)
s.set_px_per_mm(300.0 / 500.0)
print "1px + 500mm =", 1 + s, "px" # the result should be 301 pixels

The answer in increment int object was
to build my own class with all the int’s methods. So I tried this:

class UiSize2(object):
    def __init__(self, value=0):
        self._int_value = int(value)

    def __add__(self, other):
        return self._int_value.__add__(other)

    def set_px_per_mm(self, px_per_mm):
        self._int_value = int(round(self._int_value * px_per_mm))

s = UiSize2(500)
s.set_px_per_mm(300.0 / 500.0)
print "500mm + 1 =", s + 1, "px"

I works for ‘s + 1’, but for ‘1 + s’ I get a TypeError:

>>> print "1 + 500mm =", 1 + s, "px"
TypeError: unsupported operand type(s) for +: 'int' and 'UiSize2'
  • 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-12T10:54:40+00:00Added an answer on June 12, 2026 at 10:54 am

    You need to define the __radd__ magic method (“right add”) to control behavior when your custom type is on the right hand side of an addition. You’ll need to do the same for __rmul__, __rsub__, etc. to give right-hand versions of all operations.

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

Sidebar

Related Questions

I need a class that works like this: >>> a=Foo() >>> b=Foo() >>> c=Foo()
I need to create a class that will store a unique object elements. I
I need to define a C++ class that generate unique identifiers. The idea is
Hi i am working on web service application in android.i need a class that
I need a component/class that throttles execution of some method to maximum M calls
I need to build a class that will represent a row in some table
I have a class that I need to perform some actions on but I
I have a class that I need to be able to serialize to a
I have a service class that I need to execute a method on a
I have a simple Java class that I need to serialize to be stored

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.