A little test for you Guys😜

Akira Li 4kir4.1i at gmail.com
Tue Sep 22 21:48:42 CEST 2015


Python_Teacher via Python-list <python-list at python.org> writes:

...
> Let's define the function plural :
>
> def plural(words):
>     plurals = []
>     for word in words:
>        plurals.append(word + 's')
>     return plurals
>
> for word in plural(['cabagge','owl','toy']):
>     print word

plural() should accept a single word. To handle list of words, call
map(plural, words)

...
> def str2print(f):
>     def str2print_wrap(*args, **kwargs):
>         """wrapper"""
>         s = f(*args, **kwargs)
>         print s
>    return str2print_wrap
>
> def hello(s):
>     """ Return "Hello $s" """
>     return "%s %s" % ("Hello", s)

Use functools.wraps() to preserve the function info for introspection:

  import functools
  
  def prints_result(function):
      @functools.wraps(function)
      def wrapper(*args, **kwargs):
          result = function(*args, **kwargs)
          print(result)
          return result #XXX return
      return wrapper
  
  @prints_result
  def hello(...):
      pass



More information about the Python-list mailing list