Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. __author__ = 'f1ashhimself@gmail.com'
  4.  
  5. import functools
  6.  
  7.  
  8. def overload(*args):
  9.     def wrapper(fn):
  10.         global __overload_map__
  11.         if '__overload_map__' not in globals():
  12.             __overload_map__ = {}
  13.         __overload_map__[(fn.__name__, args)] = fn
  14.  
  15.         @functools.wraps(fn)
  16.         def real_wrapper(self, *args, **kwargs):
  17.             new_fn = __overload_map__[(fn.__name__,
  18.                                        tuple([type(arg) for arg in args]))]
  19.             return new_fn(self, *args, **kwargs)
  20.         return real_wrapper
  21.     return wrapper
  22.  
  23.  
  24. class Foo(object):
  25.  
  26.     @overload(str)
  27.     def bar(self, f):
  28.         print(f)
  29.  
  30.     @overload(int)
  31.     def bar(self, f):
  32.         print('Foo bar -> %s' % f)
  33.  
  34. foo = Foo()
  35. foo.bar('Something')
  36. foo.bar(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement