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