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