-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PadamSethia/PadamSethia-patch-2
Added sum_decorator.py
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|