Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python: Decorating Your Code

Python: Decorating Your Code

Deriving decorators, presented at a Pymug Meetup

Abdur-Rahmaan Janhangeer

March 31, 2019
Tweet

More Decks by Abdur-Rahmaan Janhangeer

Other Decks in Programming

Transcript

  1. Functions can be nested [1] functions can be defined within

    functions def calc(a, b): def add(a, b): return a + b return add(a, b) * 2 print(calc(2, 2))
  2. Functions can be nested [2] functions can be defined within

    functions multiple times def calc(a, b): def add(a, b): return a + b def minus(a, b): return a - b return add(a, b) * 2 print(calc(2, 2)) #8
  3. Functions can be nested [3] functions can be defined within

    functions at multiple levels def do_this(): def calc(a, b): def add(a, b): return a+b return add(a, b) * 2 return calc(1, 3) print(do_this()) # 8
  4. Functions can take functions as arguments [1] a normal function

    def print_these(): print('----') print('....') print('----')
  5. Functions can take functions as arguments [2] def print_these(): print('----')

    print('....') print('----') calling / executing it print_these() the ( ) calls the function
  6. Functions can take functions as arguments [3] implementing a fuction

    to execute other functions. it actually calls the function we pass in as argument def execute(f): f() applying def print_these(): print('----') print('....') print('----') execute(print_these) # same as print_these() # ---- # .... # ----
  7. Functions can take functions as arguments [4] we can also

    retrieve values def name(): return 'moris' def view_value(v): print('the value is', v()) view_value(name) # the value is moris
  8. Functions can return functions [1] returning a fuction: def x():

    def y(): print(3) return y calling x()() # 3
  9. Functions can return functions [3] example of use def welcome_message():

    def first_part(): return '------' def last_part(): return '******' def body(): return 'welcome to our program' def main(): print(first_part()) print(body()) print(last_part()) return main w = welcome_message() w()
  10. Getting arguments passed: positional [1] we can get all arguments

    passed using *args def s(*args): return args print(s(1, 2, 3)) print(s(1, 2)) print(s(1)) # (1, 2, 3) # (1, 2) # (1,)
  11. Getting arguments passed: positional [2] but we can change *args

    to anything like *canne def s(*canne): return canne print(s(1, 2, 3)) print(s(1, 2)) print(s(1)) # (1, 2, 3) # (1, 2) # (1,)
  12. Getting arguments passed: positional [3] can be useful in the

    case of def add(*nums): return sum(nums) print(add(1, 2, 3, 4, 5)) print(add(100, 400, 1000)) # 15 # 1500
  13. Getting arguments passed: keyword [1] kwargs allows us to get

    all keyword arguments passed def s(**kwargs): return kwargs print(s(name='me', age=5, country='mauritius')) # {'name': 'me', 'age': 5, 'country': 'mauritius'}
  14. Getting arguments passed: keyword [2] as with *args we can

    change the name to **keyword_arguments def s(**keyword_arguments): return keyword_arguments print(s(name='me', age=5, country='mauritius')) # {'name': 'me', 'age': 5, 'country': 'mauritius'}
  15. We can also mix them def view_args(*args, **keyw_args): print(args) print(keyw_args)

    view_args(1, 2, 3, name='me', town='pl') # (1, 2, 3) # {'name': 'me', 'town': 'pl'}
  16. Functions can be reassigned names [1] we can change function

    names by reassignment def add(x, y): return x + y addition = add print(addition(2, 3)) # 5
  17. Functions can be reassigned names [2] still works if we

    delete original def add(x, y): return x + y addition = add del add print(addition(2, 3)) # 5
  18. In enters the skeleton let us take this piece of

    code def quote(text): return '<<{}>>'.format(text) def indent(text): return '> {}'.format(text) print(indent(quote('abc'))) # > <<abc>> # # quote('abc') -> '<<abc>>' # indent(quote('abc')) -> '> <<abc>>'
  19. In enters the skeleton we can also write it as

    def quote(text): return '<<{}>>'.format(text) def indent(q): def dummy(text): return '> {}'.format( q(text) ) return dummy print( indent(quote)('i am here') ) # > <<i am here>>
  20. In enters the skeleton which is equivalent to: def indent(q):

    def dummy(text): return '> {}'.format( q(text) ) return dummy @indent def quote(text): return '<<{}>>'.format(text) print(quote('the sun is rising')) neater
  21. Chaining operators [1] let's say we want to get #

    ---- # > <<the sun is rising>> # ---- we just add another function def enclose(f): def dummy(text): return '----\n{}\n----'.format( f(text) ) return dummy and just call @enclose
  22. Chaining operators [2] def enclose(f): def dummy(text): return '----\n{}\n----'.format( f(text)

    ) return dummy def indent(q): def dummy(text): return '> {}'.format( q(text) ) return dummy @enclose @indent def quote(text): return '<<{}>>'.format(text) print(quote('the sun is rising'))
  23. Adding arguments to decorators def awesome_f(dec_param): def awesome_f_decorator(f): # wrap

    here def awesome_f_wrapper(p): # dec_param f(p) return awesome_f_wrapper return awesome_f_decorator @awesome_f('abcd') def some_func(): # ...
  24. bulletproofing our decorators [1] accept all arguments def indent(q): def

    dummy(*args, **kwargs): return '> {}'.format( q(*args, **kwargs) ) return dummy
  25. bulletproofing our decorators [1] preserve info from functools import wraps

    # ... def indent(q): @wraps(func) def dummy(*args, **kwargs): return '> {}'.format( q(*args, **kwargs) ) return dummy
  26. where decorators are used: @staticmethod [1] let's take a simple

    class import math class Calcs: def add(self, x, y): return x + y def hypotenuse(self, x, y): return math.sqrt((x**2) + (y**2)) c = Calcs() print(c.hypotenuse(3, 4)) # 5.0 functions not related together
  27. where decorators are used: @staticmethod [2] adding @staticmethod import math

    class Calcs: @staticmethod def add(x, y): return x + y def hypotenuse(self, x, y): return math.sqrt((x**2) + (y**2)) c = Calcs() print(c.add(1, 2)) # 3 isolated from class, using another method/var results in error
  28. where decorators are used: @staticmethod [3] use of @staticmethod isolate

    function group related functions under a name space visually telling purpose of function
  29. where decorators are used: @classmethod [2] demo import math class

    Person: @classmethod def say_hi(cls, name): return 'hi ' + name print(Person.say_hi('doe')) # hi doe
  30. where decorators are used: @property [1] another way of customising

    getters, setters and deleters class Car: def __init__(self): self._wheel = None @property def wheel(self): return self._wheel @wheel.setter def wheel(self, number): self._wheel = number @wheel.getter def wheel(self): return self._wheel @wheel.deleter def wheel(self): del self._wheel
  31. nissan = Car() nissan.wheel = 4 print(nissan.wheel) #4 but if

    getter changed @wheel.getter def wheel(self): return self._wheel + 1 and printed nissan = Car() nissan.wheel = 4 print(nissan.wheel) we'd get 5
  32. yeah, they are all functions @property , @staticmethod , @classmethod

    are all functions, in-built ones. can be used as property() , staticmethod() and classmethod . try help on them print(help(property))