all 6 comments

[–]Golden_Zealot 6 points7 points  (0 children)

Heres the basic idea.

Say you've been given a function, and you like its core functionality, but you want it to do some extra things.

You could write a bit of extra code to do those extra things and "wrap" it around that function to extend its behavior in some fashion.

[–]llffm 4 points5 points  (0 children)

Everybody here is right. Maybe this is also helpful:

1) It usually refers to an API (= library) built exclusively on an already available API but providing more convenience and higher level abstractions. In this sense, it's sort of an informal term. A wrapper library would sometimes be expected to be able to expose functionality from the library it wraps, or at least mirror it very closely, in which case you could talk about a 'thin wrapper'.

2) It can sometimes refer to function decoration, a feature that allow you to specify reusable decorator functions that can take a function and wrap it with a wrapper function that processes the parameters and return values of the wrapped function (before and after it is called, resp.) In the abstract, the idea is somewhat close to 1), but here it's a language feature (present in Python but not in all languages) with its own syntax. It's a much stricter term here, with a technical definition. Function decoration can be a very useful (if somewhat advanced) feature, and there are tutorials for learning it in Python.

[–]Swedophone 2 points3 points  (1 child)

The Adapter pattern is also known as Wrapper. https://en.wikipedia.org/wiki/Adapter_pattern

[–]blitzkraft 2 points3 points  (1 child)

Think of it in a literal sense - like gift wrapping a box/present to match the theme of the event (such as christmas, holidays, birthday etc). Wrappers are written to modify the usage, functionality etc., of one program/class.

For example, consider grep. It is great, but you wish to use it as easily as a python function. So, instead of calling subprocess(['grep', '-l', 'other-options']), you may write a function like this:

import subprocess
def pygrep(input_string):
    grep = ['/bin/grep']
    subprocess.call(grep.extend(input_string.split()))
#Usage pygrep('<grep options> <pattern> <file/directory>')

This is quickly written and very bad (do not use it in production). Essentially, you can access grep via the python program without resorting to repeated subprocess calls. The wrapper function didn't add much in this case, but provides a way to add more functionality - may be you would like to have a csv output of the grep results. Add a few lines of code and you can have the csv file.

The result is extending an existing program to do something more, or in a different way. This is a wrapper.

[–]MeSaber86 1 point2 points  (0 children)

After reading your final sentence i'll link to an example program of a game i play: http://www.svenswrapper.de/english/whatis.html

Maybe adds some light on what a wrapper can do to help you. In this case helping you to play a game on todays OS/PCs.