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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:07:15+00:00 2026-05-31T18:07:15+00:00

I would like to be able to add to a custom class in the

  • 0

I would like to be able to add to a custom class in the style of:

x=myclass("Something", 7)
x + 3

7, of course, corresponds with an inner property that I’d like to increment by adding to it.

The class holds a number that refers to a location in a list. This might seem like something that can be done by a normal integer, but I need it to act as a separate type. This is all done to emulate an old game language. The class is its ‘variable’ class, and the value of the variable is stored in the aforementioned list. Apparently, on older version of the game, arrays were faked by doing math on the variable object instance to grab a different variable. So I’m trying to emulate 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-05-31T18:07:16+00:00Added an answer on May 31, 2026 at 6:07 pm

    If you want to support addition for class instances, you need to define an __add__() method on your class:

    class MyClass(object):
        def __init__(self, x):
            self.x = x
        def __add__(self, other):
            return self.x + other
    

    Example:

    >>> a = MyClass(7)
    >>> a + 3
    10
    

    To also support 3 + a, define the __radd__() method.

    If you want to be able to update the x attribute of MyClass instances using

    a += 3
    

    you can define __iadd__().

    If you want class instances to behave like integers with some additional methods and attributes, you should simply derive from int.

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

Sidebar

Related Questions

I would like to be able to add custom snippets of javascript to any
I would like to be able to add a message to a unit test,
I would like to be able to add a new SQL LOGIN and name
I would like to be able to add a click event to a entity
I would like to add a uitoolbar able to slide -in from the top
I would like to be able to use custom string querying within my NHibernate
I would like to add a control to a master page (that will go
I would like to be able to add a hidden form field using array
I am making a custom widget that I would like to use in multiple
Would like to be able to set colors of headings and such, different font

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.