Tools, FAQ, Tutorials:
What Is Wrapper Function
What is a wrapper function in Python?
✍: FYIcenter.com
A wrapper function in Python is a function that takes
an old function as input parameter and returns a new function.
The new function should have the same parameter list and return type.
If you call the new function, it will perform the same task as the old function and do some more activities.
The Python code below shows you a good example of a wrapper function called "wrapper()". It return a newDump() function when call it as wrapper(dump). The newDump() calls the old dump() and adds XML output:
>>> def dump(user):
... print(user)
...
>>> def wrapper(dump):
... def newDump(user):
... x = dump(user)
... print("<root>")
... for item in user:
... print("<"+item+">"+str(user[item])+"</"+item+">")
... print("</root>")
... return x
... return newDump
...
>>> guest = {"name": "Joe", "age": 25}
>>> dump(guest)
{'name': 'Joe', 'age': 25}
>>>
>>> dumpWrapped = wrapper(dump)
>>> dumpWrapped(guest)
{'name': 'Joe', 'age': 25}
<root>
<name>Joe</name>
<age>25</age>
</root>
You can say that dumpWrapped() is the new version of dump() modified by the wrapper function wrapper().
⇐ Pass Function as Function Parameter
2018-02-08, ∼3103🔥, 0💬
Popular Posts:
How to access Query String parameters from "context.Request.Url.Que ry"object in Azure API Policy? Q...
Tools, FAQ, Tutorials: JSON Validator JSON-XML Converter XML-JSON Converter JSON FAQ/Tutorials Pytho...
How to use the built-in "context" object in Policy expressions? The built-in "context" object can be...
How to use 'choose ... when ..." policy statements to control execution flows? If you want to contro...
How to use the JSON to XML Conversion Tool at freeformatter.com? If you want to try the JSON to XML ...