Post: [Python] Operator Overloading
05-23-2011, 08:57 PM #1
schnzrs
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({}); This is how to customize operators for your own objects in Python. In Python, there are special methods that will always have two under scores, like this:
    def __special__(self):
dostuff()

You will use special methods (__init__ and __del__) for making a constuctor or destructor. But there are more special methods. These methods usually tell an object what to do when added, subtracted, multiplied, etc... Since Python treats all data structures as objects, these methods are like the building blocks of Python. An integer will have a __add__ method that tells Python what to do when the user types in something like 1 + 1. But, since the int type (or any other Python type) is built in, you can't overload operators. If you make your own objects however, you can overload operators.

Here is a basic class I made that should help explain it. When initialized, this object can be added with integers, even though it stores a string. Cool, huh? That's the power of overloading operators.
    class strnum:
"""A class designed to show overloading operators in Python. This class is basic and is for holding strings that can be mixed with integers. These are not all the special functions, just the most important ones."""
def __init__(self, string):
int(string) # Just a test to see if string is a number.
string[0] # A test to see if it is a string with a length <= 1
self.string = string

def __add__(self, other):
"""This changes the addition + operator"""
return int(self.string) + int(other)

def __div__(self, other):
"""This changes the division / operator"""
return int(self.string) / int(other)

def __mul__(self, other):
"""This changes the multiplication * operator"""
return int(self.string) * int(other)

def __pow__(self, other):
"""This changes the exponent ** operator"""
return int(self.string) ** int(other)

def __str__(self):
"""This changes what appears when this object is printed"""
return self.string

def __sub__(self, other):
"""This changes the subtraction - operator"""
return int(self.string) - int(other)
(adsbygoogle = window.adsbygoogle || []).push({});

The following 2 users say thank you to schnzrs for this useful post:

Relevant, vSaBoTeuR x
05-24-2011, 02:35 PM #2
vSaBoTeuR x
< ^ > < ^ >
Nice! Do you get to learn Python at school? I have to do it with C++ now, in 2 years we'll get Java.
05-24-2011, 05:30 PM #3
Rath
Today Will Be Different
Hmm I've never played with Python... but there seems to be some similarities between C#. The way the syntax is, it look identical, except different integers, arrays etc.
05-24-2011, 09:47 PM #4
Love you for this Smile
05-25-2011, 01:16 AM #5
kiwimoosical
Bounty hunter
Originally posted by RathHavoc View Post
Hmm I've never played with Python... but there seems to be some similarities between C#. The way the syntax is, it look identical, except different integers, arrays etc.


Lol, this is like the exact opposite syntax of C#.
Btw, this isn't operator overloading, this is operator creation. Operator overloading would be like make + do multiple things. Just sayin.
05-25-2011, 01:37 AM #6
Rath
Today Will Be Different
Originally posted by kiwimoosical View Post
Lol, this is like the exact opposite syntax of C#.
Btw, this isn't operator overloading, this is operator creation. Operator overloading would be like make + do multiple things. Just sayin.


Syntax was probably the wrong word to use :lol: I should have said like if you can code C# you can code Python and vis versa. ;p lol
05-25-2011, 03:49 AM #7
schnzrs
Do a barrel roll!
Originally posted by kiwimoosical View Post
Lol, this is like the exact opposite syntax of C#.
Btw, this isn't operator overloading, this is operator creation. Operator overloading would be like make + do multiple things. Just sayin.

Well Python's official documentation is calling it overloading, so that's what I'm going by. You must login or register to view this content.
Thanks for the heads up though.

---------- Post added at 11:49 PM ---------- Previous post was at 11:07 PM ----------

Originally posted by xNCK View Post
Nice! Do you get to learn Python at school? I have to do it with C++ now, in 2 years we'll get Java.

Yeah, my school has a 1 year course on Python. I think when I am a senior (in a little more than a year), my school will have a course on Objective-C (iPhone programming).

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo