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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:05:38+00:00 2026-06-18T06:05:38+00:00

This isn’t for anything I’m working on yet, it’s just some test code as

  • 0

This isn’t for anything I’m working on yet, it’s just some test code as I’m just learning class methods and suck. But say I have the following code

class Test(int):
    def __init__(self,arg):
            self = arg

    def thing(self):
            self += 10

and going,
foo=Test(12) sets foo to 12. However I want it, so when I do, foo.thing(), foo increases by 10. So far, going foo.thing() just keeps it at 12. How would I change this code to do that.

  • 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-18T06:05:40+00:00Added an answer on June 18, 2026 at 6:05 am

    Because int is a immutable, you cannot magically turn it into a mutable type.

    Your methods are no-ops. They change self in the local namespace, by reassigning it to something else. They no longer point to the instance. In other words, both methods leave the original instance unaltered.

    You cannot do what you want to do with a subclass of int. You’ll have to create a custom class from scratch using the numeric type hooks instead.

    Background: int has a __new__ method that assigns the actual value to self, and there is no __iadd__ method to support in-place adding. The __init__ method is not ignored, but you can leave it out altogether since __new__ already did the work.

    Assigning to self means you just replaced the reference to the instance with something else, you didn’t alter anything about self:

    >>> class Foo(int):
    ...     def __init__(self, value=0):
    ...         print self, type(self)
    ...         self = 'foobar'
    ...         print type(self)
    ... 
    >>> foo = Foo(10)
    10 <class '__main__.Foo'>
    <type 'str'>
    >>> print foo, type(foo)
    10 <class '__main__.Foo'>
    

    Because int does not have a __iadd__ in-place add method, your self += 2 is interpreted as self = self + 2 instead; again, you are assigning to self and replacing it with a new value altogether.

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

Sidebar

Related Questions

This isn't my code, I'm just troubleshooting it. Some code that has been working
This isn't a code question for once, but it definitely has me confused. Basically,
This isn't valid code: public class MyClass { private static boolean yesNo = false;
This isn't working the way I thought it would. Here are my classes: class
(This isn't a homework question, as it's above and beyond the class just for
This isn't the complete code, but it's the parts that are causing problems. I've
This isn't actually homework, I'm just looking through some questions in a discrete maths
This isn't for anything serious, more so to just play around and experiment. Just
This isn't a question on proper coding practice, I'm just working through the semantics.
This isn't the best question ever, but since search engines feel the need 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.