Decorator toy code throws syntax errors - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Decorator toy code throws syntax errors (/thread-4703.html) |
Decorator toy code throws syntax errors - kevinxhi - Sep-03-2017 I am a newbie trying to learn programming. Here is my decorator toy code. It throws syntax error when I call the argument function. def boldtext(fn):"""This is the 1st decorator function""" result = "<b>" + fn + "</b>" return result def italicizetext(fn): """This is the 2nd decorator function""" result = "<i>" + fn() + "</i>" return result @boldtext @italicizetext def printsampletext(): """This is the argument function""" return "Some sample text!" printsampletext() RE: Decorator toy code throws syntax errors - jogl - Sep-03-2017 Check out Harrison's tutorial on decorator's here: https://pythonprogramming.net/decorators-intermediate-python-tutorial/ RE: Decorator toy code throws syntax errors - wavic - Sep-04-2017 def decorator(function): """The decorator function""" def wrapper(text): print("Decorated function ahead") function(text) print("Decorated function behind") return wrapper @decorator def func(text): print(text) func("Decorated function") Here is how to do a decorator. You can change completely the function behaviour that way. Instead, printing its own string it prints two more.
RE: Decorator toy code throws syntax errors - kevinxhi - Sep-04-2017 Thanks to both Tim and Bishop. |