Aigars Aigars wrote:
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.


Thank you,
Aigars
------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
First of all, you should always inherit from "object" whenever it is possible.


Then the answer:  you did not return the result.

Instead of

               self.func(*args, **kwargs)

use this:


               return self.func(*args, **kwargs)

Corrected example attached.

Best,

  Laszlo


class Logger(object):
	def __init__(self, function):
		self.func = function

	def __call__(self, *args, **kwargs):
		print "Function %s called with args = %s, kwargs = %s" % (self.func.__name__, str(args), str(kwargs))
                return self.func(*args, **kwargs) # Return is important here!
	    
@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
	
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to