Creating my own "integer" object in Python

create your own logo
best free logo maker
how to make your own website for free
free logo design and download
wix
free logo design templates
free logo download
creating a website for your business

Essentially I want to be able to do something like:

a = Integer(1)
a += 1
print a

And of course printing the number two as result. What methods do I need to create to get this behaviour in my Integer class?

Disclaimer: I'm not planning to use this for "real", just curious.

This is a simple and incomplete example. Look at methods __sub__, __div__ and so on.

class Integer(object):
    def __init__(self, val=0):
        self._val = int(val)
    def __add__(self, val):
        if isinstance(val, Integer):
            return Integer(self._val + val._val)
        return self._val + val
    def __iadd__(self, val):
        self._val += val
        return self
    def __str__(self):
        return str(self._val)
    def __repr__(self):
        return 'Integer(%s)' % self._val

Then

n = Integer()
print n
m = Integer(7)
m+=5
print m

EDIT fixed __repr__ and added __iadd__. Thanks to @Keith for pointing problems out. EDIT Fixed __add__ to allow addition between Integers.

FreeLogoDesign: Logo Maker, Create your logo design online for your business or project. Its Free to use. Customize a logo for your company easily with our free online logo maker. Create Your Own. Personalize your own Filters and Lenses! Whether it's a Filter that frames the moments at a friend's wedding, or a Lens that makes birthdays even more hilarious, your custom creations will make any event more special.

First, take a quick look at the documentation in the reference manual on emulating numeric types.

(Don't get too stuck on that - it's just to give you some familiarity with the methods underlying arithmetic operations in Python)

Then refer to the documentation for the numbers module, which includes all the Abstract Base Classes that are most relevant to emulating different kinds of numbers (e.g. numbers.Integral for custom integers).

Wix.com: Free Website Builder, Design your own professional logo and build your brand online. Mobile optimized website with a blonde man standing on the beach shore with a surfboard. Our online design tool allows entrepreneurs, small businesses, freelancers and associations around the world to create professional looking logos in minutes. Create your own logo design with Free Logo Design, 100% free, fast and effective! Via our logo creator in 3-steps you can create and upload your own logo, in minutes.

You can use operator overloading:

class Integer:

  def __init__(self, value):
    self.value = value

  def __repr__(self):
    return str(self.value)

  def __add__(self, value):
    self.value += value
    return self

a = Integer(2)
print a

a = a+3
print a

a += 4
print a

Logo Maker | Create Your Own Free Logo Design, Create a logo with our online logo maker. Just answer a few questions so we get to know your brand, and you'll get a design that's right for you. Create your own free avatar online and share it with your friends! Avatar Maker Create Your Own Avatar for Free. Generate random numbers with Number Generator

If you want to overload operators of the default cast-to-string method, the phrase you're looking for is "magic methods". These are the methods named like "__<name>__" and are used by python in cases other than direct method calls. You would want to define the __add__ and __str__ methods for your class in order for lines 2 and 3, respectively, to work.

Its worth mentioning that the __add__ method will be called if your new type is the left operand, and any type may be passed as its argument. For cases when yours is the right operand, you should also define the __radd__ method. This goes for all of the binary operators.

For a more complete list of magic methods for a numeric type, see Emulating Numeric Types.

How to Create a Website: Step-by-Step Guide for Beginners (2020), With your bare website alive and kicking, it's now time to make it feel more like your own by picking  Create & design your logo for free using an easy logo maker tool. Choose from hundreds of fonts and icons. Then just save your new logo on to your computer! Watch our video tutorial on how to create your logo.

I assume you want your Integer class to be mutable. To get your example, this will work:

class Integer(object):
    def __init__(self, num):
        self._val = num

    def __iadd__(self, other):
        self._val += int(other)

    def __str__(self):
        return str(self._val)

Webnode: Create a free website easily, You can create and own your website for free for unlimited time. Make a website at Webnode in a matter of minutes! Creating your own font has never been easier. Transform your handwriting and calligraphy into fully functional vector fonts with our web application. Calligraphr - Draw your own fonts.

Free Online Flyer Maker: Design Custom Flyers With Canva, How to create a beautiful flyer in under 5 minutes. Choose a flyer template (​Canva has thousands!) or you can design your own flyer from scratch. These are​  Create your Website Here. Builderall has all the tools and training. Easy, Fast and Professional! Conversion Focus and monitor your progress easily.

Create Your Own, Your Logo Here. Some of the best ad space exists on the backsides of our phones. Promote your brand by giving away PopSockets products customized with 

Design your own custom clothes!, Custom Clothing Products Create Get your creative juices flowing! Bring out the designer in you to design your clothes on your own! Start Creating Now.

Comments
  • Also see stackoverflow.com/questions/1638229.
  • Please read this: docs.python.org/library/numbers.html#module-numbers. After reading that, please update your question to be more specific.
  • @FM thanks, that was good!
  • This example mis-uses __repr__. That is intended to return the inverse of eval() and should return Integer(val) in this case. It also doesn't maintain the type: Python2> m = Integer(7) Python2> type(m) <class '__main__.Integer'> Python2> m+=5 Python2> type(m) <type 'int'>
  • You should be inheriting from numbers.Number, not object; as per ncoghlan answer below. If you want to delete unwanted inherited methods, that's easy.