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
  Function throws error but then works? Milfredo 10 495 Sep-12-2020, 05:16 AM
Last Post: Milfredo
  decorator adamfairhall 0 128 Aug-18-2020, 08:38 AM
Last Post: adamfairhall
  Syntax error in python code sample ErnestTBass 5 337 Aug-14-2020, 07:14 PM
Last Post: deanhystad
  2 errors in code numbnumb54 2 219 Jul-24-2020, 02:28 PM
Last Post: Marbelous
  python/winrt Bluetooth.GenericAttributProfile CreateAsync(GUID) method throws Not Imp pbvinoth 0 243 Jul-08-2020, 04:27 AM
Last Post: pbvinoth
  Code errors rhyjom 0 250 Jun-21-2020, 04:50 PM
Last Post: rhyjom
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 2 388 Jun-14-2020, 01:53 PM
Last Post: hobbyist
  Use of @property decorator ruy 16 682 Jun-09-2020, 05:29 PM
Last Post: buran
  Pip Syntax Errors in CMD: Windows 10 and Python 3.8.1 jamesphopper 2 1,335 Feb-08-2020, 07:21 PM
Last Post: jamesphopper
  Errors in code coolcassie 2 340 Dec-03-2019, 12:10 AM
Last Post: coolcassie

Forum Jump:


Users browsing this thread: 1 Guest(s)