Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Decorator toy code throws syntax errors
#1
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()
Quote
#2
Check out Harrison's tutorial on decorator's here:

https://pythonprogramming.net/decorators...-tutorial/
Quote
#3
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")
Output:
Decorated function ahead Decorated function Decorated function behind
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#4
Thanks to both Tim and Bishop.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pip Syntax Errors in CMD: Windows 10 and Python 3.8.1 jamesphopper 2 268 Feb-08-2020, 07:21 PM
Last Post: jamesphopper
  Errors in code coolcassie 2 141 Dec-03-2019, 12:10 AM
Last Post: coolcassie
  Errors trying to run code ziggyztz 3 232 Nov-03-2019, 04:24 AM
Last Post: newbieAuggie2019
  errors in code sylvie1987100 3 329 Sep-11-2019, 12:54 PM
Last Post: sylvie1987100
  Code syntax with underscore and assigning to a tuple santoshbwn 1 285 Jun-24-2019, 12:05 PM
Last Post: ThomasL
  Decorator staticmethod Use Cases Devarishi 3 376 May-20-2019, 04:27 AM
Last Post: Devarishi
  How can we override decorator? bhojendra 2 382 May-12-2019, 11:15 PM
Last Post: ichabod801
  Syntax Error when running code codeman 3 687 Apr-02-2019, 11:47 AM
Last Post: codeman
  Decorator question Dan741 1 587 Nov-14-2018, 10:05 AM
Last Post: wavic
  Can't find error in code but Python throws exception Sandwich_masterX 3 568 Oct-09-2018, 01:38 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)