Skip to content

Commit

Permalink
Merge pull request #2 from PadamSethia/PadamSethia-patch-2
Browse files Browse the repository at this point in the history
Added sum_decorator.py
  • Loading branch information
highoncarbs committed Oct 9, 2017
2 parents cdf18e7 + 77fca5f commit 3e724b2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions SimpleAddition/sum_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Most python newbies may not be knowing about decorators
# They are inbuit python funcitons also know as magic function
# They have a general format like __self__ , __SOMETHING__ etc.

# So for this script we'll be using Pyhton decorators to add
# numbers

# 1. Simple Addition
print (5).__add__(7) # prints 12

print (2).__add__(1) # prints 3

# 2. Functions

class Number(int):
'''
i have not use '+' to add self and other as '+' references to
__add__ , hence it would create an infinite recursion.
So we used two '-' to add the two numbers.
'''
def __add__(self, other):
return self - (-other)

print Number(5) + 3 # Prints 8

0 comments on commit 3e724b2

Please sign in to comment.