Skip to content

Instantly share code, notes, and snippets.

@sayz
Created January 17, 2016 22:09
Show Gist options
  • Select an option

  • Save sayz/c39901918cfb6d6dc5ef to your computer and use it in GitHub Desktop.

Select an option

Save sayz/c39901918cfb6d6dc5ef to your computer and use it in GitHub Desktop.
dekoratörler için örnek olarak kenarda dursun
# -*- coding: utf-8 -*-
#def add(x, y):
# return x + y
#def sub(x, y):
# return x - y
#def apply(func, x, y):
# return func(x, y)
#print apply(add, 5, 2)
#print apply(sub, 5, 2)
def outer(some_func):
def inner():
print "before some_func"
ret = some_func()
return ret + 1
return inner
def foo():
return 1
#decorated = outer(foo)
#foo = outer(foo)
#print foo()
class Coordinate(object):
"""docstring for Coordinate"""
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "Coord: " + str(self.__dict__)
def wrapper(func):
def checker(a, b): # burada argümanları kontrol ediyoruz
if a.x < 0 or a.y < 0:
a = Coordinate(a.x if a.x > 0 else 0, a.y if a.y > 0 else 0)
if b.x < 0 or b.y < 0:
b = Coordinate(b.x if b.x > 0 else 0, b.y if b.y > 0 else 0)
ret = func(a, b)
if ret.x < 0 or ret.y < 0:
ret = Coordinate(
ret.x if ret.x > 0 else 0,
ret.y if ret.y > 0 else 0)
return ret
return checker
one = Coordinate(100, 200)
two = Coordinate(300, 200)
three = Coordinate(-100, -100)
# fonksiyonları sarmalamaya başlıyoruz burada
@wrapper # add = wrapper(add) işlemi ile aynı şey
def add(a, b):
return Coordinate(a.x + b.x, a.y + b.y)
@wrapper # sub = wrapper(sub) işlemi ile aynı şey
def sub(a, b):
return Coordinate(a.x - b.x, a.y - b.y)
print sub(one, two)
print add(one, three)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment