Function decorators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aigars Aigars

    Function decorators

    Good day all,

    I am learning Python and came up to decorators.

    Thequestion is: Why does function FoodList return value None?

    The code in attachment.
    Thank you,
    Aigars
  • David C. Ullrich

    #2
    Re: Function decorators

    In article <mailman.493.12 20542975.3487.p ython-list@python.org >,
    Aigars Aigars <srad@inbox.lvw rote:
    Good day all,
    I am learning Python and came up to decorators.

    The question
    is: Why does function FoodList return value None?
    Because the function doesn't return anything, and in Python
    a function that doesn't explicitly return anything returns None.
    >
    The code in attachment.


    Thanks for not just pasting it into the post, like so:

    class Logger:

    def __init__(self, function):

    self.func = function



    def __call__(self, *args, **kwargs):

    print "Function %s called with args = %s, kwargs = %s" %
    (self.func.__na me__, str(args), str(kwargs))

    self.func(*args , **kwargs)



    @Logger

    def FoodList(a, b, c="spam"):

    text = "Food is %s, %s, %s" % (a, b, c)

    print text

    return text





    if __name__ == "__main__":

    a = FoodList("eggs" , "potatoes")

    print a
    Thank you,
    Aigars---------------------------------------------------------------------
    [Image]
    --
    David C. Ullrich

    Comment

    • Diez B. Roggisch

      #3
      Re: Function decorators

      Aigars Aigars schrieb:
      Good day all,
      >
      I am learning Python and came up to decorators.
      >
      The question is: Why does function FoodList return value None?
      >
      The code in attachment.
      Because the __call__ in Logger doesn't return the value of self.func.

      Diez

      Comment

      Working...