• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1## partial.make
2
3<pre>
4partial.make(<a href="#partial.make-func">func</a>, <a href="#partial.make-args">args</a>, <a href="#partial.make-kwargs">kwargs</a>)
5</pre>
6
7Creates a partial that can be called using `call`.
8
9A partial can have args assigned to it at the make site, and can have args
10passed to it at the call sites.
11
12A partial 'function' can be defined with positional args and kwargs:
13
14  # function with no args
15  def function1():
16    ...
17
18  # function with 2 args
19  def function2(arg1, arg2):
20    ...
21
22  # function with 2 args and keyword args
23  def function3(arg1, arg2, x, y):
24    ...
25
26The positional args passed to the function are the args passed into make
27followed by any additional positional args given to call. The below example
28illustrates a function with two positional arguments where one is supplied by
29make and the other by call:
30
31  # function demonstrating 1 arg at make site, and 1 arg at call site
32  def _foo(make_arg1, func_arg1):
33  print(make_arg1 + " " + func_arg1 + "!")
34
35For example:
36
37  hi_func = partial.make(_foo, "Hello")
38  bye_func = partial.make(_foo, "Goodbye")
39  partial.call(hi_func, "Jennifer")
40  partial.call(hi_func, "Dave")
41  partial.call(bye_func, "Jennifer")
42  partial.call(bye_func, "Dave")
43
44prints:
45
46  "Hello, Jennifer!"
47  "Hello, Dave!"
48  "Goodbye, Jennifer!"
49  "Goodbye, Dave!"
50
51The keyword args given to the function are the kwargs passed into make
52unioned with the keyword args given to call. In case of a conflict, the
53keyword args given to call take precedence. This allows you to set a default
54value for keyword arguments and override it at the call site.
55
56Example with a make site arg, a call site arg, a make site kwarg and a
57call site kwarg:
58
59  def _foo(make_arg1, call_arg1, make_location, call_location):
60    print(make_arg1 + " is from " + make_location + " and " +
61          call_arg1 + " is from " + call_location + "!")
62
63  func = partial.make(_foo, "Ben", make_location="Hollywood")
64  partial.call(func, "Jennifer", call_location="Denver")
65
66Prints "Ben is from Hollywood and Jennifer is from Denver!".
67
68  partial.call(func, "Jennifer", make_location="LA", call_location="Denver")
69
70Prints "Ben is from LA and Jennifer is from Denver!".
71
72Note that keyword args may not overlap with positional args, regardless of
73whether they are given during the make or call step. For instance, you can't
74do:
75
76def foo(x):
77  pass
78
79func = partial.make(foo, 1)
80partial.call(func, x=2)
81
82
83### Parameters
84
85<table class="params-table">
86  <colgroup>
87    <col class="col-param" />
88    <col class="col-description" />
89  </colgroup>
90  <tbody>
91    <tr id="partial.make-func">
92      <td><code>func</code></td>
93      <td>
94        required.
95        <p>
96          The function to be called.
97        </p>
98      </td>
99    </tr>
100    <tr id="partial.make-args">
101      <td><code>args</code></td>
102      <td>
103        optional.
104        <p>
105          Positional arguments to be passed to function.
106        </p>
107      </td>
108    </tr>
109    <tr id="partial.make-kwargs">
110      <td><code>kwargs</code></td>
111      <td>
112        optional.
113        <p>
114          Keyword arguments to be passed to function. Note that these can
115          be overridden at the call sites.
116        </p>
117      </td>
118    </tr>
119  </tbody>
120</table>
121
122
123## partial.call
124
125<pre>
126partial.call(<a href="#partial.call-partial">partial</a>, <a href="#partial.call-args">args</a>, <a href="#partial.call-kwargs">kwargs</a>)
127</pre>
128
129Calls a partial created using `make`.
130
131### Parameters
132
133<table class="params-table">
134  <colgroup>
135    <col class="col-param" />
136    <col class="col-description" />
137  </colgroup>
138  <tbody>
139    <tr id="partial.call-partial">
140      <td><code>partial</code></td>
141      <td>
142        required.
143        <p>
144          The partial to be called.
145        </p>
146      </td>
147    </tr>
148    <tr id="partial.call-args">
149      <td><code>args</code></td>
150      <td>
151        optional.
152        <p>
153          Additional positional arguments to be appended to the ones given to
154       make.
155        </p>
156      </td>
157    </tr>
158    <tr id="partial.call-kwargs">
159      <td><code>kwargs</code></td>
160      <td>
161        optional.
162        <p>
163          Additional keyword arguments to augment and override the ones
164          given to make.
165        </p>
166      </td>
167    </tr>
168  </tbody>
169</table>
170
171
172