Question

Using Python, create a prefix Decorator function (or Closure) over the print function. The decorator (which...

Using Python, create a prefix Decorator function (or Closure) over the print function. The decorator (which takes two parameters) shall return a new function that accepts a single parameter string. When called and supplied with this string, the decorated print function shall first print its own, internal prefix followed by the supplied input.

For example:

first = Decorator("Joe:")

second = Decorator("Carl:")

first("Are you hungry")

second("yes")

Which would produce:

Joe: Are you hungry

Carl: yes

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def Decorator(prefix):
    def fn(s):
        print(prefix, s)

    return fn


first = Decorator("Joe:")
second = Decorator("Carl:")
first("Are you hungry")
second("yes")

Add a comment
Know the answer?
Add Answer to:
Using Python, create a prefix Decorator function (or Closure) over the print function. The decorator (which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT