• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Starlark in Go: Language definition
2
3Starlark is a dialect of Python intended for use as a configuration
4language.  A Starlark interpreter is typically embedded within a larger
5application, and this application may define additional
6domain-specific functions and data types beyond those provided by the
7core language.  For example, Starlark is embedded within (and was
8originally developed for) the [Bazel build tool](https://bazel.build),
9and [Bazel's build language](https://docs.bazel.build/versions/master/starlark/language.html) is based on Starlark.
10
11This document describes the Go implementation of Starlark
12at go.starlark.net/starlark.
13The language it defines is similar but not identical to
14[the Java-based implementation](https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/starlark/Starlark.java)
15used by Bazel.
16We identify places where their behaviors differ, and an
17[appendix](#dialect-differences) provides a summary of those
18differences.
19We plan to converge both implementations on a single specification.
20
21This document is maintained by Alan Donovan <adonovan@google.com>.
22It was influenced by the Python specification,
23Copyright 1990&ndash;2017, Python Software Foundation,
24and the Go specification, Copyright 2009&ndash;2017, The Go Authors.
25
26Starlark was designed and implemented in Java by Laurent Le Brun,
27Dmitry Lomov, Jon Brandvin, and Damien Martin-Guillerez, standing on
28the shoulders of the Python community.
29The Go implementation was written by Alan Donovan and Jay Conrod;
30its scanner was derived from one written by Russ Cox.
31
32## Overview
33
34Starlark is an untyped dynamic language with high-level data types,
35first-class functions with lexical scope, and automatic memory
36management or _garbage collection_.
37
38Starlark is strongly influenced by Python, and is almost a subset of
39that language.  In particular, its data types and syntax for
40statements and expressions will be very familiar to any Python
41programmer.
42However, Starlark is intended not for writing applications but for
43expressing configuration: its programs are short-lived and have no
44external side effects and their main result is structured data or side
45effects on the host application.
46As a result, Starlark has no need for classes, exceptions, reflection,
47concurrency, and other such features of Python.
48
49Starlark execution is _deterministic_: all functions and operators
50in the core language produce the same execution each time the program
51is run; there are no sources of random numbers, clocks, or unspecified
52iterators. This makes Starlark suitable for use in applications where
53reproducibility is paramount, such as build tools.
54
55## Contents
56
57<!-- WTF? No automatic TOC? -->
58
59  * [Overview](#overview)
60  * [Contents](#contents)
61  * [Lexical elements](#lexical-elements)
62  * [Data types](#data-types)
63    * [None](#none)
64    * [Booleans](#booleans)
65    * [Integers](#integers)
66    * [Floating-point numbers](#floating-point-numbers)
67    * [Strings](#strings)
68    * [Lists](#lists)
69    * [Tuples](#tuples)
70    * [Dictionaries](#dictionaries)
71    * [Sets](#sets)
72    * [Functions](#functions)
73    * [Built-in functions](#built-in-functions)
74  * [Name binding and variables](#name-binding-and-variables)
75  * [Value concepts](#value-concepts)
76    * [Identity and mutation](#identity-and-mutation)
77    * [Freezing a value](#freezing-a-value)
78    * [Hashing](#hashing)
79    * [Sequence types](#sequence-types)
80    * [Indexing](#indexing)
81  * [Expressions](#expressions)
82    * [Identifiers](#identifiers)
83    * [Literals](#literals)
84    * [Parenthesized expressions](#parenthesized-expressions)
85    * [Dictionary expressions](#dictionary-expressions)
86    * [List expressions](#list-expressions)
87    * [Unary operators](#unary-operators)
88    * [Binary operators](#binary-operators)
89    * [Conditional expressions](#conditional-expressions)
90    * [Comprehensions](#comprehensions)
91    * [Function and method calls](#function-and-method-calls)
92    * [Dot expressions](#dot-expressions)
93    * [Index expressions](#index-expressions)
94    * [Slice expressions](#slice-expressions)
95    * [Lambda expressions](#lambda-expressions)
96  * [Statements](#statements)
97    * [Pass statements](#pass-statements)
98    * [Assignments](#assignments)
99    * [Augmented assignments](#augmented-assignments)
100    * [Function definitions](#function-definitions)
101    * [Return statements](#return-statements)
102    * [Expression statements](#expression-statements)
103    * [If statements](#if-statements)
104    * [For loops](#for-loops)
105    * [Break and Continue](#break-and-continue)
106    * [Load statements](#load-statements)
107    * [Module execution](#module-execution)
108  * [Built-in constants and functions](#built-in-constants-and-functions)
109    * [None](#none)
110    * [True and False](#true-and-false)
111    * [any](#any)
112    * [all](#all)
113    * [bool](#bool)
114    * [chr](#chr)
115    * [dict](#dict)
116    * [dir](#dir)
117    * [enumerate](#enumerate)
118    * [fail](#fail)
119    * [float](#float)
120    * [getattr](#getattr)
121    * [hasattr](#hasattr)
122    * [hash](#hash)
123    * [int](#int)
124    * [len](#len)
125    * [list](#list)
126    * [max](#max)
127    * [min](#min)
128    * [ord](#ord)
129    * [print](#print)
130    * [range](#range)
131    * [repr](#repr)
132    * [reversed](#reversed)
133    * [set](#set)
134    * [sorted](#sorted)
135    * [str](#str)
136    * [tuple](#tuple)
137    * [type](#type)
138    * [zip](#zip)
139  * [Built-in methods](#built-in-methods)
140    * [dict·clear](#dict·clear)
141    * [dict·get](#dict·get)
142    * [dict·items](#dict·items)
143    * [dict·keys](#dict·keys)
144    * [dict·pop](#dict·pop)
145    * [dict·popitem](#dict·popitem)
146    * [dict·setdefault](#dict·setdefault)
147    * [dict·update](#dict·update)
148    * [dict·values](#dict·values)
149    * [list·append](#list·append)
150    * [list·clear](#list·clear)
151    * [list·extend](#list·extend)
152    * [list·index](#list·index)
153    * [list·insert](#list·insert)
154    * [list·pop](#list·pop)
155    * [list·remove](#list·remove)
156    * [set·union](#set·union)
157    * [string·capitalize](#string·capitalize)
158    * [string·codepoint_ords](#string·codepoint_ords)
159    * [string·codepoints](#string·codepoints)
160    * [string·count](#string·count)
161    * [string·elem_ords](#string·elem_ords)
162    * [string·elems](#string·elems)
163    * [string·endswith](#string·endswith)
164    * [string·find](#string·find)
165    * [string·format](#string·format)
166    * [string·index](#string·index)
167    * [string·isalnum](#string·isalnum)
168    * [string·isalpha](#string·isalpha)
169    * [string·isdigit](#string·isdigit)
170    * [string·islower](#string·islower)
171    * [string·isspace](#string·isspace)
172    * [string·istitle](#string·istitle)
173    * [string·isupper](#string·isupper)
174    * [string·join](#string·join)
175    * [string·lower](#string·lower)
176    * [string·lstrip](#string·lstrip)
177    * [string·partition](#string·partition)
178    * [string·replace](#string·replace)
179    * [string·rfind](#string·rfind)
180    * [string·rindex](#string·rindex)
181    * [string·rpartition](#string·rpartition)
182    * [string·rsplit](#string·rsplit)
183    * [string·rstrip](#string·rstrip)
184    * [string·split](#string·split)
185    * [string·splitlines](#string·splitlines)
186    * [string·startswith](#string·startswith)
187    * [string·strip](#string·strip)
188    * [string·title](#string·title)
189    * [string·upper](#string·upper)
190  * [Dialect differences](#dialect-differences)
191
192
193## Lexical elements
194
195A Starlark program consists of one or more modules.
196Each module is defined by a single UTF-8-encoded text file.
197
198A complete grammar of Starlark can be found in [grammar.txt](../syntax/grammar.txt).
199That grammar is presented piecemeal throughout this document
200in boxes such as this one, which explains the notation:
201
202```grammar {.good}
203Grammar notation
204
205- lowercase and 'quoted' items are lexical tokens.
206- Capitalized names denote grammar productions.
207- (...) implies grouping.
208- x | y means either x or y.
209- [x] means x is optional.
210- {x} means x is repeated zero or more times.
211- The end of each declaration is marked with a period.
212```
213
214The contents of a Starlark file are broken into a sequence of tokens of
215five kinds: white space, punctuation, keywords, identifiers, and literals.
216Each token is formed from the longest sequence of characters that
217would form a valid token of each kind.
218
219```grammar {.good}
220File = {Statement | newline} eof .
221```
222
223*White space* consists of spaces (U+0020), tabs (U+0009), carriage
224returns (U+000D), and newlines (U+000A).  Within a line, white space
225has no effect other than to delimit the previous token, but newlines,
226and spaces at the start of a line, are significant tokens.
227
228*Comments*: A hash character (`#`) appearing outside of a string
229literal marks the start of a comment; the comment extends to the end
230of the line, not including the newline character.
231Comments are treated like other white space.
232
233*Punctuation*: The following punctuation characters or sequences of
234characters are tokens:
235
236```text
237+    -    *    /    //   %    =
238+=   -=   *=   /=   //=  %=   ==   !=
239^    <    >    <<   >>   &    |
240^=   <=   >=   <<=  >>=  &=   |=
241.    ,    ;    :    ~    **
242(    )    [    ]    {    }
243```
244
245*Keywords*: The following tokens are keywords and may not be used as
246identifiers:
247
248```text
249and            elif           in             or
250break          else           lambda         pass
251continue       for            load           return
252def            if             not            while
253```
254
255The tokens below also may not be used as identifiers although they do not
256appear in the grammar; they are reserved as possible future keywords:
257
258<!-- and to remain a syntactic subset of Python -->
259
260```text
261as             finally        nonlocal
262assert         from           raise
263class          global         try
264del            import         with
265except         is             yield
266```
267
268<b>Implementation note:</b>
269The Go implementation permits `assert` to be used as an identifier,
270and this feature is widely used in its tests.
271
272*Identifiers*: an identifier is a sequence of Unicode letters, decimal
273 digits, and underscores (`_`), not starting with a digit.
274Identifiers are used as names for values.
275
276Examples:
277
278```text
279None    True    len
280x       index   starts_with     arg0
281```
282
283*Literals*: literals are tokens that denote specific values.  Starlark
284has string, integer, and floating-point literals.
285
286```text
2870                               # int
288123                             # decimal int
2890x7f                            # hexadecimal int
2900o755                           # octal int
2910b1011                          # binary int
292
2930.0     0.       .0             # float
2941e10    1e+10    1e-10
2951.1e10  1.1e+10  1.1e-10
296
297"hello"      'hello'            # string
298'''hello'''  """hello"""        # triple-quoted string
299r'hello'     r"hello"           # raw string literal
300```
301
302Integer and floating-point literal tokens are defined by the following grammar:
303
304```grammar {.good}
305int         = decimal_lit | octal_lit | hex_lit | binary_lit .
306decimal_lit = ('1' … '9') {decimal_digit} | '0' .
307octal_lit   = '0' ('o'|'O') octal_digit {octal_digit} .
308hex_lit     = '0' ('x'|'X') hex_digit {hex_digit} .
309binary_lit  = '0' ('b'|'B') binary_digit {binary_digit} .
310
311float     = decimals '.' [decimals] [exponent]
312          | decimals exponent
313          | '.' decimals [exponent]
314          .
315decimals  = decimal_digit {decimal_digit} .
316exponent  = ('e'|'E') ['+'|'-'] decimals .
317
318decimal_digit = '0' … '9' .
319octal_digit   = '0' … '7' .
320hex_digit     = '0' … '9' | 'A' … 'F' | 'a' … 'f' .
321binary_digit  = '0' | '1' .
322```
323
324### String literals
325
326A Starlark string literal denotes a string value.
327In its simplest form, it consists of the desired text
328surrounded by matching single- or double-quotation marks:
329
330```python
331"abc"
332'abc'
333```
334
335Literal occurrences of the chosen quotation mark character must be
336escaped by a preceding backslash. So, if a string contains several
337of one kind of quotation mark, it may be convenient to quote the string
338using the other kind, as in these examples:
339
340```python
341'Have you read "To Kill a Mockingbird?"'
342"Yes, it's a classic."
343
344"Have you read \"To Kill a Mockingbird?\""
345'Yes, it\'s a classic.'
346```
347
348Literal occurrences of the _opposite_ kind of quotation mark, such as
349an apostrophe within a double-quoted string literal, may be escaped
350by a backslash, but this is not necessary: `"it's"` and `"it\'s"` are
351equivalent.
352
353
354#### String escapes
355
356Within a string literal, the backslash character `\` indicates the
357start of an _escape sequence_, a notation for expressing things that
358are impossible or awkward to write directly.
359
360The following *traditional escape sequences* represent the ASCII control
361codes 7-13:
362
363```
364\a   \x07 alert or bell
365\b   \x08 backspace
366\f   \x0C form feed
367\n   \x0A line feed
368\r   \x0D carriage return
369\t   \x09 horizontal tab
370\v   \x0B vertical tab
371```
372
373A *literal backslash* is written using the escape `\\`.
374
375An *escaped newline*---that is, a backslash at the end of a line---is ignored,
376allowing a long string to be split across multiple lines of the source file.
377
378```python
379"abc\
380def"			# "abcdef"
381```
382
383An *octal escape* encodes a single byte using its octal value.
384It consists of a backslash followed by one, two, or three octal digits [0-7].
385It is error if the value is greater than decimal 255.
386
387```python
388'\0'			# "\x00"  a string containing a single NUL byte
389'\12'			# "\n"    octal 12 = decimal 10
390'\101-\132'		# "A-Z"
391'\119'			# "\t9"   = "\11" + "9"
392```
393
394<b>Implementation note:</b>
395The Java implementation encodes strings using UTF-16,
396so an octal escape encodes a single UTF-16 code unit.
397Octal escapes for values above 127 are therefore not portable across implementations.
398There is little reason to use octal escapes in new code.
399
400A *hex escape* encodes a single byte using its hexadecimal value.
401It consists of `\x` followed by exactly two hexadecimal digits [0-9A-Fa-f].
402
403```python
404"\x00"			# "\x00"  a string containing a single NUL byte
405"(\x20)"		# "( )"   ASCII 0x20 = 32 = space
406
407red, reset = "\x1b[31m", "\x1b[0m"	# ANSI terminal control codes for color
408"(" + red + "hello" + reset + ")"	# "(hello)" with red text, if on a terminal
409```
410
411<b>Implementation note:</b>
412The Java implementation does not support hex escapes.
413
414An ordinary string literal may not contain an unescaped newline,
415but a *multiline string literal* may spread over multiple source lines.
416It is denoted using three quotation marks at start and end.
417Within it, unescaped newlines and quotation marks (or even pairs of
418quotation marks) have their literal meaning, but three quotation marks
419end the literal. This makes it easy to quote large blocks of text with
420few escapes.
421
422```
423haiku = '''
424Yesterday it worked.
425Today it is not working.
426That's computers. Sigh.
427'''
428```
429
430Regardless of the platform's convention for text line endings---for
431example, a linefeed (\n) on UNIX, or a carriage return followed by a
432linefeed (\r\n) on Microsoft Windows---an unescaped line ending in a
433multiline string literal always denotes a line feed (\n).
434
435Starlark also supports *raw string literals*, which look like an
436ordinary single- or double-quotation preceded by `r`. Within a raw
437string literal, there is no special processing of backslash escapes,
438other than an escaped quotation mark (which denotes a literal
439quotation mark), or an escaped newline (which denotes a backslash
440followed by a newline). This form of quotation is typically used when
441writing strings that contain many quotation marks or backslashes (such
442as regular expressions or shell commands) to reduce the burden of
443escaping:
444
445```python
446"a\nb"		# "a\nb"  = 'a' + '\n' + 'b'
447r"a\nb"		# "a\\nb" = 'a' + '\\' + 'n' + 'b'
448
449"a\
450b"		# "ab"
451r"a\
452b"		# "a\\\nb"
453```
454
455It is an error for a backslash to appear within a string literal other
456than as part of one of the escapes described above.
457
458TODO: define indent, outdent, semicolon, newline, eof
459
460## Data types
461
462These are the main data types built in to the interpreter:
463
464```python
465NoneType                     # the type of None
466bool                         # True or False
467int                          # a signed integer of arbitrary magnitude
468float                        # an IEEE 754 double-precision floating point number
469string                       # a byte string
470list                         # a modifiable sequence of values
471tuple                        # an unmodifiable sequence of values
472dict                         # a mapping from values to values
473set                          # a set of values
474function                     # a function implemented in Starlark
475builtin_function_or_method   # a function or method implemented by the interpreter or host application
476```
477
478Some functions, such as the iteration methods of `string`, or the
479`range` function, return instances of special-purpose types that don't
480appear in this list.
481Additional data types may be defined by the host application into
482which the interpreter is embedded, and those data types may
483participate in basic operations of the language such as arithmetic,
484comparison, indexing, and function calls.
485
486<!-- We needn't mention the stringIterable type here. -->
487
488Some operations can be applied to any Starlark value.  For example,
489every value has a type string that can be obtained with the expression
490`type(x)`, and any value may be converted to a string using the
491expression `str(x)`, or to a Boolean truth value using the expression
492`bool(x)`.  Other operations apply only to certain types.  For
493example, the indexing operation `a[i]` works only with strings, lists,
494and tuples, and any application-defined types that are _indexable_.
495The [_value concepts_](#value-concepts) section explains the groupings of
496types by the operators they support.
497
498
499### None
500
501`None` is a distinguished value used to indicate the absence of any other value.
502For example, the result of a call to a function that contains no return statement is `None`.
503
504`None` is equal only to itself.  Its [type](#type) is `"NoneType"`.
505The truth value of `None` is `False`.
506
507
508### Booleans
509
510There are two Boolean values, `True` and `False`, representing the
511truth or falsehood of a predicate.  The [type](#type) of a Boolean is `"bool"`.
512
513Boolean values are typically used as conditions in `if`-statements,
514although any Starlark value used as a condition is implicitly
515interpreted as a Boolean.
516For example, the values `None`, `0`, `0.0`, and the empty sequences
517`""`, `()`, `[]`, and `{}` have a truth value of `False`, whereas non-zero
518numbers and non-empty sequences have a truth value of `True`.
519Application-defined types determine their own truth value.
520Any value may be explicitly converted to a Boolean using the built-in `bool`
521function.
522
523```python
5241 + 1 == 2                              # True
5252 + 2 == 5                              # False
526
527if 1 + 1:
528        print("True")
529else:
530        print("False")
531```
532
533### Integers
534
535The Starlark integer type represents integers.  Its [type](#type) is `"int"`.
536
537Integers may be positive or negative, and arbitrarily large.
538Integer arithmetic is exact.
539Integers are totally ordered; comparisons follow mathematical
540tradition.
541
542The `+` and `-` operators perform addition and subtraction, respectively.
543The `*` operator performs multiplication.
544
545The `//` and `%` operations on integers compute floored division and
546remainder of floored division, respectively.
547If the signs of the operands differ, the sign of the remainder `x % y`
548matches that of the divisor, `y`.
549For all finite x and y (y ≠ 0), `(x // y) * y + (x % y) == x`.
550The `/` operator implements real division, and
551yields a `float` result even when its operands are both of type `int`.
552
553Integers, including negative values, may be interpreted as bit vectors.
554The `|`, `&`, and `^` operators implement bitwise OR, AND, and XOR,
555respectively. The unary `~` operator yields the bitwise inversion of its
556integer argument. The `<<` and `>>` operators shift the first argument
557to the left or right by the number of bits given by the second argument.
558
559Any bool, number, or string may be interpreted as an integer by using
560the `int` built-in function.
561
562An integer used in a Boolean context is considered true if it is
563non-zero.
564
565```python
566100 // 5 * 9 + 32               # 212
5673 // 2                          # 1
5683 / 2                           # 1.5
569111111111 * 111111111           # 12345678987654321
570"0x%x" % (0x1234 & 0xf00f)      # "0x1004"
571int("ffff", 16)                 # 65535, 0xffff
572```
573
574### Floating-point numbers
575
576The Starlark floating-point data type represents an IEEE 754
577double-precision floating-point number.  Its [type](#type) is `"float"`.
578
579Arithmetic on floats using the `+`, `-`, `*`, `/`, `//`, and `%`
580 operators follows the IEE 754 standard.
581However, computing the division or remainder of division by zero is a dynamic error.
582
583An arithmetic operation applied to a mixture of `float` and `int`
584operands works as if the `int` operand is first converted to a
585`float`.  For example, `3.141 + 1` is equivalent to `3.141 +
586float(1)`.
587There are two floating-point division operators:
588`x / y ` yields the floating-point quotient of `x` and `y`,
589whereas `x // y` yields `floor(x / y)`, that is, the largest
590integer value not greater than `x / y`.
591Although the resulting number is integral, it is represented as a
592`float` if either operand is a `float`.
593
594The `%` operation computes the remainder of floored division.
595As with the corresponding operation on integers,
596if the signs of the operands differ, the sign of the remainder `x % y`
597matches that of the divisor, `y`.
598
599The infinite float values `+Inf` and `-Inf` represent numbers
600greater/less than all finite float values.
601
602The non-finite `NaN` value represents the result of dubious operations
603such as `Inf/Inf`.  A NaN value compares neither less than, nor
604greater than, nor equal to any value, including itself.
605
606All floats other than NaN are totally ordered, so they may be compared
607using operators such as `==` and `<`.
608
609Any bool, number, or string may be interpreted as a floating-point
610number by using the `float` built-in function.
611
612A float used in a Boolean context is considered true if it is
613non-zero.
614
615```python
6161.23e45 * 1.23e45                               # 1.5129e+90
6171.111111111111111 * 1.111111111111111           # 1.23457
6183.0 / 2                                         # 1.5
6193 / 2.0                                         # 1.5
620float(3) / 2                                    # 1.5
6213.0 // 2.0                                      # 1
622```
623
624### Strings
625
626A string represents an immutable sequence of bytes.
627The [type](#type) of a string is `"string"`.
628
629Strings can represent arbitrary binary data, including zero bytes, but
630most strings contain text, encoded by convention using UTF-8.
631
632The built-in `len` function returns the number of bytes in a string.
633
634Strings may be concatenated with the `+` operator.
635
636The substring expression `s[i:j]` returns the substring of `s` from
637index `i` up to index `j`.  The index expression `s[i]` returns the
6381-byte substring `s[i:i+1]`.
639
640Strings are hashable, and thus may be used as keys in a dictionary.
641
642Strings are totally ordered lexicographically, so strings may be
643compared using operators such as `==` and `<`.
644
645Strings are _not_ iterable sequences, so they cannot be used as the operand of
646a `for`-loop, list comprehension, or any other operation than requires
647an iterable sequence.
648To obtain a view of a string as an iterable sequence of numeric byte
649values, 1-byte substrings, numeric Unicode code points, or 1-code
650point substrings, you must explicitly call one of its four methods:
651`elems`, `elem_ords`, `codepoints`, or `codepoint_ords`.
652
653Any value may formatted as a string using the `str` or `repr` built-in
654functions, the `str % tuple` operator, or the `str.format` method.
655
656A string used in a Boolean context is considered true if it is
657non-empty.
658
659Strings have several built-in methods:
660
661* [`capitalize`](#string·capitalize)
662* [`codepoint_ords`](#string·codepoint_ords)
663* [`codepoints`](#string·codepoints)
664* [`count`](#string·count)
665* [`elem_ords`](#string·elem_ords)
666* [`elems`](#string·elems)
667* [`endswith`](#string·endswith)
668* [`find`](#string·find)
669* [`format`](#string·format)
670* [`index`](#string·index)
671* [`isalnum`](#string·isalnum)
672* [`isalpha`](#string·isalpha)
673* [`isdigit`](#string·isdigit)
674* [`islower`](#string·islower)
675* [`isspace`](#string·isspace)
676* [`istitle`](#string·istitle)
677* [`isupper`](#string·isupper)
678* [`join`](#string·join)
679* [`lower`](#string·lower)
680* [`lstrip`](#string·lstrip)
681* [`partition`](#string·partition)
682* [`replace`](#string·replace)
683* [`rfind`](#string·rfind)
684* [`rindex`](#string·rindex)
685* [`rpartition`](#string·rpartition)
686* [`rsplit`](#string·rsplit)
687* [`rstrip`](#string·rstrip)
688* [`split`](#string·split)
689* [`splitlines`](#string·splitlines)
690* [`startswith`](#string·startswith)
691* [`strip`](#string·strip)
692* [`title`](#string·title)
693* [`upper`](#string·upper)
694
695<b>Implementation note:</b>
696The type of a string element varies across implementations.
697There is agreement that byte strings, with text conventionally encoded
698using UTF-8, is the ideal choice, but the Java implementation treats
699strings as sequences of UTF-16 codes and changing it appears
700intractible; see Google Issue b/36360490.
701
702<b>Implementation note:</b>
703The Java implementation does not consistently treat strings as
704iterable; see `testdata/string.star` in the test suite and Google Issue
705b/34385336 for further details.
706
707### Lists
708
709A list is a mutable sequence of values.
710The [type](#type) of a list is `"list"`.
711
712Lists are indexable sequences: the elements of a list may be iterated
713over by `for`-loops, list comprehensions, and various built-in
714functions.
715
716List may be constructed using bracketed list notation:
717
718```python
719[]              # an empty list
720[1]             # a 1-element list
721[1, 2]          # a 2-element list
722```
723
724Lists can also be constructed from any iterable sequence by using the
725built-in `list` function.
726
727The built-in `len` function applied to a list returns the number of elements.
728The index expression `list[i]` returns the element at index i,
729and the slice expression `list[i:j]` returns a new list consisting of
730the elements at indices from i to j.
731
732List elements may be added using the `append` or `extend` methods,
733removed using the `remove` method, or reordered by assignments such as
734`list[i] = list[j]`.
735
736The concatenation operation `x + y` yields a new list containing all
737the elements of the two lists x and y.
738
739For most types, `x += y` is equivalent to `x = x + y`, except that it
740evaluates `x` only once, that is, it allocates a new list to hold
741the concatenation of `x` and `y`.
742However, if `x` refers to a list, the statement does not allocate a
743new list but instead mutates the original list in place, similar to
744`x.extend(y)`.
745
746Lists are not hashable, so may not be used in the keys of a dictionary.
747
748A list used in a Boolean context is considered true if it is
749non-empty.
750
751A [_list comprehension_](#comprehensions) creates a new list whose elements are the
752result of some expression applied to each element of another sequence.
753
754```python
755[x*x for x in [1, 2, 3, 4]]      # [1, 4, 9, 16]
756```
757
758A list value has these methods:
759
760* [`append`](#list·append)
761* [`clear`](#list·clear)
762* [`extend`](#list·extend)
763* [`index`](#list·index)
764* [`insert`](#list·insert)
765* [`pop`](#list·pop)
766* [`remove`](#list·remove)
767
768### Tuples
769
770A tuple is an immutable sequence of values.
771The [type](#type) of a tuple is `"tuple"`.
772
773Tuples are constructed using parenthesized list notation:
774
775```python
776()                      # the empty tuple
777(1,)                    # a 1-tuple
778(1, 2)                  # a 2-tuple ("pair")
779(1, 2, 3)               # a 3-tuple
780```
781
782Observe that for the 1-tuple, the trailing comma is necessary to
783distinguish it from the parenthesized expression `(1)`.
7841-tuples are seldom used.
785
786Starlark, unlike Python, does not permit a trailing comma to appear in
787an unparenthesized tuple expression:
788
789```python
790for k, v, in dict.items(): pass                 # syntax error at 'in'
791_ = [(v, k) for k, v, in dict.items()]          # syntax error at 'in'
792f = lambda a, b, : None                         # syntax error at ':'
793
794sorted(3, 1, 4, 1,)                             # ok
795[1, 2, 3, ]                                     # ok
796{1: 2, 3:4, }                                   # ok
797```
798
799Any iterable sequence may be converted to a tuple by using the
800built-in `tuple` function.
801
802Like lists, tuples are indexed sequences, so they may be indexed and
803sliced.  The index expression `tuple[i]` returns the tuple element at
804index i, and the slice expression `tuple[i:j]` returns a sub-sequence
805of a tuple.
806
807Tuples are iterable sequences, so they may be used as the operand of a
808`for`-loop, a list comprehension, or various built-in functions.
809
810Unlike lists, tuples cannot be modified.
811However, the mutable elements of a tuple may be modified.
812
813Tuples are hashable (assuming their elements are hashable),
814so they may be used as keys of a dictionary.
815
816Tuples may be concatenated using the `+` operator.
817
818A tuple used in a Boolean context is considered true if it is
819non-empty.
820
821
822### Dictionaries
823
824A dictionary is a mutable mapping from keys to values.
825The [type](#type) of a dictionary is `"dict"`.
826
827Dictionaries provide constant-time operations to insert an element, to
828look up the value for a key, or to remove an element.  Dictionaries
829are implemented using hash tables, so keys must be hashable.  Hashable
830values include `None`, Booleans, numbers, and strings, and tuples
831composed from hashable values.  Most mutable values, such as lists,
832dictionaries, and sets, are not hashable, even when frozen.
833Attempting to use a non-hashable value as a key in a dictionary
834results in a dynamic error.
835
836A [dictionary expression](#dictionary-expressions) specifies a
837dictionary as a set of key/value pairs enclosed in braces:
838
839```python
840coins = {
841  "penny": 1,
842  "nickel": 5,
843  "dime": 10,
844  "quarter": 25,
845}
846```
847
848The expression `d[k]`, where `d` is a dictionary and `k` is a key,
849retrieves the value associated with the key.  If the dictionary
850contains no such item, the operation fails:
851
852```python
853coins["penny"]          # 1
854coins["dime"]           # 10
855coins["silver dollar"]  # error: key not found
856```
857
858The number of items in a dictionary `d` is given by `len(d)`.
859A key/value item may be added to a dictionary, or updated if the key
860is already present, by using `d[k]` on the left side of an assignment:
861
862```python
863len(coins)				# 4
864coins["shilling"] = 20
865len(coins)				# 5, item was inserted
866coins["shilling"] = 5
867len(coins)				# 5, existing item was updated
868```
869
870A dictionary can also be constructed using a [dictionary
871comprehension](#comprehension), which evaluates a pair of expressions,
872the _key_ and the _value_, for every element of another iterable such
873as a list.  This example builds a mapping from each word to its length
874in bytes:
875
876```python
877words = ["able", "baker", "charlie"]
878{x: len(x) for x in words}	# {"charlie": 7, "baker": 5, "able": 4}
879```
880
881Dictionaries are iterable sequences, so they may be used as the
882operand of a `for`-loop, a list comprehension, or various built-in
883functions.
884Iteration yields the dictionary's keys in the order in which they were
885inserted; updating the value associated with an existing key does not
886affect the iteration order.
887
888```python
889x = dict([("a", 1), ("b", 2)])          # {"a": 1, "b": 2}
890x.update([("a", 3), ("c", 4)])          # {"a": 3, "b": 2, "c": 4}
891```
892
893```python
894for name in coins:
895  print(name, coins[name])	# prints "quarter 25", "dime 10", ...
896```
897
898Like all mutable values in Starlark, a dictionary can be frozen, and
899once frozen, all subsequent operations that attempt to update it will
900fail.
901
902A dictionary used in a Boolean context is considered true if it is
903non-empty.
904
905Dictionaries may be compared for equality using `==` and `!=`.  Two
906dictionaries compare equal if they contain the same number of items
907and each key/value item (k, v) found in one dictionary is also present
908in the other.  Dictionaries are not ordered; it is an error to compare
909two dictionaries with `<`.
910
911
912A dictionary value has these methods:
913
914* [`clear`](#dict·clear)
915* [`get`](#dict·get)
916* [`items`](#dict·items)
917* [`keys`](#dict·keys)
918* [`pop`](#dict·pop)
919* [`popitem`](#dict·popitem)
920* [`setdefault`](#dict·setdefault)
921* [`update`](#dict·update)
922* [`values`](#dict·values)
923
924### Sets
925
926A set is a mutable set of values.
927The [type](#type) of a set is `"set"`.
928
929Like dictionaries, sets are implemented using hash tables, so the
930elements of a set must be hashable.
931
932Sets may be compared for equality or inequality using `==` and `!=`.
933Two sets compare equal if they contain the same elements.
934
935Sets are iterable sequences, so they may be used as the operand of a
936`for`-loop, a list comprehension, or various built-in functions.
937Iteration yields the set's elements in the order in which they were
938inserted.
939
940The binary `|` and `&` operators compute union and intersection when
941applied to sets.  The right operand of the `|` operator may be any
942iterable value.  The binary `in` operator performs a set membership
943test when its right operand is a set.
944
945The binary `^` operator performs symmetric difference of two sets.
946
947Sets are instantiated by calling the built-in `set` function, which
948returns a set containing all the elements of its optional argument,
949which must be an iterable sequence.  Sets have no literal syntax.
950
951The only method of a set is `union`, which is equivalent to the `|` operator.
952
953A set used in a Boolean context is considered true if it is non-empty.
954
955<b>Implementation note:</b>
956The Go implementation of Starlark requires the `-set` flag to
957enable support for sets.
958The Java implementation does not support sets.
959
960
961### Functions
962
963A function value represents a function defined in Starlark.
964Its [type](#type) is `"function"`.
965A function value used in a Boolean context is always considered true.
966
967Functions defined by a [`def` statement](#function-definitions) are named;
968functions defined by a [`lambda` expression](#lambda-expressions) are anonymous.
969
970Function definitions may be nested, and an inner function may refer to a local variable of an outer function.
971
972A function definition defines zero or more named parameters.
973Starlark has a rich mechanism for passing arguments to functions.
974
975<!-- TODO break up this explanation into caller-side and callee-side
976     parts, and put the former under function calls and the latter
977     under function definitions. Also try to convey that the Callable
978     interface sees the flattened-out args and kwargs and that's what
979     built-ins get.
980-->
981
982The example below shows a definition and call of a function of two
983required parameters, `x` and `y`.
984
985```python
986def idiv(x, y):
987  return x // y
988
989idiv(6, 3)		# 2
990```
991
992A call may provide arguments to function parameters either by
993position, as in the example above, or by name, as in first two calls
994below, or by a mixture of the two forms, as in the third call below.
995All the positional arguments must precede all the named arguments.
996Named arguments may improve clarity, especially in functions of
997several parameters.
998
999```python
1000idiv(x=6, y=3)		# 2
1001idiv(y=3, x=6)		# 2
1002
1003idiv(6, y=3)		# 2
1004```
1005
1006<b>Optional parameters:</b> A parameter declaration may specify a
1007default value using `name=value` syntax; such a parameter is
1008_optional_.  The default value expression is evaluated during
1009execution of the `def` statement or evaluation of the `lambda`
1010expression, and the default value forms part of the function value.
1011All optional parameters must follow all non-optional parameters.
1012A function call may omit arguments for any suffix of the optional
1013parameters; the effective values of those arguments are supplied by
1014the function's parameter defaults.
1015
1016```python
1017def f(x, y=3):
1018  return x, y
1019
1020f(1, 2)	# (1, 2)
1021f(1)	# (1, 3)
1022```
1023
1024If a function parameter's default value is a mutable expression,
1025modifications to the value during one call may be observed by
1026subsequent calls.
1027Beware of this when using lists or dicts as default values.
1028If the function becomes frozen, its parameters' default values become
1029frozen too.
1030
1031```python
1032# module a.star
1033def f(x, list=[]):
1034  list.append(x)
1035  return list
1036
1037f(4, [1,2,3])           # [1, 2, 3, 4]
1038f(1)                    # [1]
1039f(2)                    # [1, 2], not [2]!
1040
1041# module b.star
1042load("a.star", "f")
1043f(3)                    # error: cannot append to frozen list
1044```
1045
1046<b>Variadic functions:</b> Some functions allow callers to provide an
1047arbitrary number of arguments.
1048After all required and optional parameters, a function definition may
1049specify a _variadic arguments_ or _varargs_ parameter, indicated by a
1050star preceding the parameter name: `*args`.
1051Any surplus positional arguments provided by the caller are formed
1052into a tuple and assigned to the `args` parameter.
1053
1054```python
1055def f(x, y, *args):
1056  return x, y, args
1057
1058f(1, 2)                 # (1, 2, ())
1059f(1, 2, 3, 4)           # (1, 2, (3, 4))
1060```
1061
1062<b>Keyword-variadic functions:</b> Some functions allow callers to
1063provide an arbitrary sequence of `name=value` keyword arguments.
1064A function definition may include a final _keyword arguments_ or
1065_kwargs_ parameter, indicated by a double-star preceding the parameter
1066name: `**kwargs`.
1067Any surplus named arguments that do not correspond to named parameters
1068are collected in a new dictionary and assigned to the `kwargs` parameter:
1069
1070```python
1071def f(x, y, **kwargs):
1072  return x, y, kwargs
1073
1074f(1, 2)                 # (1, 2, {})
1075f(x=2, y=1)             # (2, 1, {})
1076f(x=2, y=1, z=3)        # (2, 1, {"z": 3})
1077```
1078
1079It is a static error if any two parameters of a function have the same name.
1080
1081Just as a function definition may accept an arbitrary number of
1082positional or named arguments, a function call may provide an
1083arbitrary number of positional or named arguments supplied by a
1084list or dictionary:
1085
1086```python
1087def f(a, b, c=5):
1088  return a * b + c
1089
1090f(*[2, 3])              # 11
1091f(*[2, 3, 7])           # 13
1092f(*[2])                 # error: f takes at least 2 arguments (1 given)
1093
1094f(**dict(b=3, a=2))             # 11
1095f(**dict(c=7, a=2, b=3))        # 13
1096f(**dict(a=2))                  # error: f takes at least 2 arguments (1 given)
1097f(**dict(d=4))                  # error: f got unexpected keyword argument "d"
1098```
1099
1100Once the parameters have been successfully bound to the arguments
1101supplied by the call, the sequence of statements that comprise the
1102function body is executed.
1103
1104It is a static error if a function call has two named arguments of the
1105same name, such as `f(x=1, x=2)`. A call that provides a `**kwargs`
1106argument may yet have two values for the same name, such as
1107`f(x=1, **dict(x=2))`. This results in a dynamic error.
1108
1109Function arguments are evaluated in the order they appear in the call.
1110<!-- see https://github.com/bazelbuild/starlark/issues/13 -->
1111
1112Unlike Python, Starlark does not allow more than one `*args` argument in a
1113call, and if a `*args` argument is present it must appear after all
1114positional and named arguments.
1115
1116The final argument to a function call may be followed by a trailing comma.
1117
1118A function call completes normally after the execution of either a
1119`return` statement, or of the last statement in the function body.
1120The result of the function call is the value of the return statement's
1121operand, or `None` if the return statement had no operand or if the
1122function completeted without executing a return statement.
1123
1124```python
1125def f(x):
1126  if x == 0:
1127    return
1128  if x < 0:
1129    return -x
1130  print(x)
1131
1132f(1)            # returns None after printing "1"
1133f(0)            # returns None without printing
1134f(-1)           # returns 1 without printing
1135```
1136
1137<b>Implementation note:</b>
1138The Go implementation of Starlark requires the `-recursion`
1139flag to allow recursive functions.
1140
1141
1142If the `-recursion` flag is not specified it is a dynamic error for a
1143function to call itself or another function value with the same
1144declaration.
1145
1146```python
1147def fib(x):
1148  if x < 2:
1149    return x
1150  return fib(x-2) + fib(x-1)	# dynamic error: function fib called recursively
1151
1152fib(5)
1153```
1154
1155This rule, combined with the invariant that all loops are iterations
1156over finite sequences, implies that Starlark programs can not be
1157Turing complete unless the `-recursion` flag is specified.
1158
1159<!-- This rule is supposed to deter people from abusing Starlark for
1160     inappropriate uses, especially in the build system.
1161     It may work for that purpose, but it doesn't stop Starlark programs
1162     from consuming too much time or space.  Perhaps it should be a
1163     dialect option.
1164-->
1165
1166
1167
1168### Built-in functions
1169
1170A built-in function is a function or method implemented in Go by the interpreter
1171or the application into which the interpreter is embedded.
1172
1173The [type](#type) of a built-in function is `"builtin_function_or_method"`.
1174
1175A built-in function value used in a Boolean context is always considered true.
1176
1177Many built-in functions are predeclared in the environment
1178(see [Name Resolution](#name-resolution)).
1179Some built-in functions such as `len` are _universal_, that is,
1180available to all Starlark programs.
1181The host application may predeclare additional built-in functions
1182in the environment of a specific module.
1183
1184Except where noted, built-in functions accept only positional arguments.
1185The parameter names serve merely as documentation.
1186
1187Most built-in functions that have a Boolean parameter require its
1188argument to be `True` or `False`. Unlike `if` statements, other values
1189are not implicitly converted to their truth value and instead cause a
1190dynamic error.
1191
1192
1193## Name binding and variables
1194
1195After a Starlark file is parsed, but before its execution begins, the
1196Starlark interpreter checks statically that the program is well formed.
1197For example, `break` and `continue` statements may appear only within
1198a loop; a `return` statement may appear only within a
1199function; and `load` statements may appear only outside any function.
1200
1201_Name resolution_ is the static checking process that
1202resolves names to variable bindings.
1203During execution, names refer to variables.  Statically, names denote
1204places in the code where variables are created; these places are
1205called _bindings_.  A name may denote different bindings at different
1206places in the program.  The region of text in which a particular name
1207refers to the same binding is called that binding's _scope_.
1208
1209Four Starlark constructs bind names, as illustrated in the example below:
1210`load` statements (`a` and `b`),
1211`def` statements (`c`),
1212function parameters (`d`),
1213and assignments (`e`, `h`, including the augmented assignment `e += 1`).
1214Variables may be assigned or re-assigned explicitly (`e`, `h`), or implicitly, as
1215in a `for`-loop (`f`) or comprehension (`g`, `i`).
1216
1217```python
1218load("lib.star", "a", b="B")
1219
1220def c(d):
1221  e = 0
1222  for f in d:
1223     print([True for g in f])
1224     e += 1
1225
1226h = [2*i for i in a]
1227```
1228
1229The environment of a Starlark program is structured as a tree of
1230_lexical blocks_, each of which may contain name bindings.
1231The tree of blocks is parallel to the syntax tree.
1232Blocks are of five kinds.
1233
1234<!-- Avoid the term "built-in" block since that's also a type. -->
1235At the root of the tree is the _predeclared_ block,
1236which binds several names implicitly.
1237The set of predeclared names includes the universal
1238constant values `None`, `True`, and `False`, and
1239various built-in functions such as `len` and `list`;
1240these functions are immutable and stateless.
1241An application may pre-declare additional names
1242to provide domain-specific functions to that file, for example.
1243These additional functions may have side effects on the application.
1244Starlark programs cannot change the set of predeclared bindings
1245or assign new values to them.
1246
1247Nested beneath the predeclared block is the _module_ block,
1248which contains the bindings of the current module.
1249Bindings in the module block (such as `c`, and `h` in the
1250example) are called _global_ and may be visible to other modules.
1251The module block is empty at the start of the file
1252and is populated by top-level binding statements.
1253
1254Nested beneath the module block is the _file_ block,
1255which contains bindings local to the current file.
1256Names in this block (such as `a` and `b` in the example)
1257are bound only by `load` statements.
1258The sets of names bound in the file block and in the module block do not overlap:
1259it is an error for a load statement to bind the name of a global,
1260or for a top-level statement to bind a name bound by a load statement.
1261
1262A file block contains a _function_ block for each top-level
1263function, and a _comprehension_ block for each top-level comprehension.
1264Bindings in either of these kinds of block,
1265and in the file block itself, are called _local_.
1266(In the example, the bindings for `e`, `f`, `g`, and `i` are all local.)
1267Additional functions and comprehensions, and their blocks, may be
1268nested in any order, to any depth.
1269
1270If name is bound anywhere within a block, all uses of the name within
1271the block are treated as references to that binding,
1272even if the use appears before the binding.
1273This is true even at the top level, unlike Python.
1274The binding of `y` on the last line of the example below makes `y`
1275local to the function `hello`, so the use of `y` in the print
1276statement also refers to the local `y`, even though it appears
1277earlier.
1278
1279```python
1280y = "goodbye"
1281
1282def hello():
1283  for x in (1, 2):
1284    if x == 2:
1285      print(y) # prints "hello"
1286    if x == 1:
1287      y = "hello"
1288```
1289It is a dynamic error to evaluate a reference to a local variable
1290before it has been bound:
1291
1292```python
1293def f():
1294  print(x)              # dynamic error: local variable x referenced before assignment
1295  x = "hello"
1296```
1297
1298The same is true for global variables:
1299
1300```python
1301print(x)                # dynamic error: global variable x referenced before assignment
1302x = "hello"
1303```
1304
1305The same is also true for nested loops in comprehensions.
1306In the (unnatural) examples below, the scope of the variables `x`, `y`,
1307and `z` is the entire compehension block, except the operand of the first
1308loop (`[]` or `[1]`), which is resolved in the enclosing environment.
1309The second loop may thus refer to variables defined by the third (`z`),
1310even though such references would fail if actually executed.
1311
1312```
1313[1//0 for x in [] for y in z for z in ()]   # []   (no error)
1314[1//0 for x in [1] for y in z for z in ()]  # dynamic error: local variable z referenced before assignment
1315```
1316
1317
1318<!-- This is similar to Python[23]. Presumed rational: it resembles
1319     the desugaring to nested loop statements, in which the scope
1320     of all three variables is the entire enclosing function,
1321     including the portion before the bindings.
1322
1323      def f():
1324        ...
1325        for x in []:
1326          for y in z:
1327            for z in ():
1328              1//0
1329-->
1330
1331It is a static error to refer to a name that has no binding at all.
1332```
1333def f():
1334  if False:
1335    g()                   # static error: undefined: g
1336```
1337(This behavior differs from Python, which treats such references as global,
1338and thus does not report an error until the expression is evaluated.)
1339
1340<!-- Consequently, the REPL, which consumes one compound statement at a time,
1341     cannot resolve forward references such as
1342             def f(): return K
1343             K = 1
1344     because the first chunk has an unresolved reference to K.
1345-->
1346
1347It is a static error to bind a global variable already explicitly bound in the file:
1348
1349```python
1350x = 1
1351x = 2                   # static error: cannot reassign global x declared on line 1
1352```
1353
1354<!-- The above rule, and the rule that forbids if-statements and loops at
1355     top level, exist to ensure that there is exactly one statement
1356     that binds each global variable, which makes cross-referenced
1357     documentation more useful, the designers assure me, but
1358     I am skeptical that it's worth the trouble. -->
1359
1360If a name was pre-bound by the application, the Starlark program may
1361explicitly bind it, but only once.
1362
1363An augmented assignment statement such as `x += y` is considered both a
1364reference to `x` and a binding use of `x`, so it may not be used at
1365top level.
1366
1367<b>Implementation note:</b>
1368The Go implementation of Starlark permits augmented assignments to appear
1369at top level if the `-globalreassign` flag is enabled.
1370
1371A function may refer to variables defined in an enclosing function.
1372In this example, the inner function `f` refers to a variable `x`
1373that is local to the outer function `squarer`.
1374`x` is a _free variable_ of `f`.
1375The function value (`f`) created by a `def` statement holds a
1376reference to each of its free variables so it may use
1377them even after the enclosing function has returned.
1378
1379```python
1380def squarer():
1381    x = [0]
1382    def f():
1383      x[0] += 1
1384      return x[0]*x[0]
1385    return f
1386
1387sq = squarer()
1388print(sq(), sq(), sq(), sq()) # "1 4 9 16"
1389```
1390
1391An inner function cannot assign to a variable bound in an enclosing
1392function, because the assignment would bind the variable in the
1393inner function.
1394In the example below, the `x += 1` statement binds `x` within `f`,
1395hiding the outer `x`.
1396Execution fails because the inner `x` has not been assigned before the
1397attempt to increment it.
1398
1399```python
1400def squarer():
1401    x = 0
1402    def f():
1403      x += 1            # dynamic error: local variable x referenced before assignment
1404      return x*x
1405    return f
1406
1407sq = squarer()
1408```
1409
1410(Starlark has no equivalent of Python's `nonlocal` or `global`
1411declarations, but as the first version of `squarer` showed, this
1412omission can be worked around by using a list of a single element.)
1413
1414
1415A name appearing after a dot, such as `split` in
1416`get_filename().split('/')`, is not resolved statically.
1417The [dot expression](#dot-expressions) `.split` is a dynamic operation
1418on the value returned by `get_filename()`.
1419
1420
1421## Value concepts
1422
1423Starlark has eleven core [data types](#data-types).  An application
1424that embeds the Starlark intepreter may define additional types that
1425behave like Starlark values.  All values, whether core or
1426application-defined, implement a few basic behaviors:
1427
1428```text
1429str(x)		-- return a string representation of x
1430type(x)		-- return a string describing the type of x
1431bool(x)		-- convert x to a Boolean truth value
1432```
1433
1434### Identity and mutation
1435
1436Starlark is an imperative language: programs consist of sequences of
1437statements executed for their side effects.
1438For example, an assignment statement updates the value held by a
1439variable, and calls to some built-in functions such as `print` change
1440the state of the application that embeds the interpreter.
1441
1442Values of some data types, such as `NoneType`, `bool`, `int`, `float`, and
1443`string`, are _immutable_; they can never change.
1444Immutable values have no notion of _identity_: it is impossible for a
1445Starlark program to tell whether two integers, for instance, are
1446represented by the same object; it can tell only whether they are
1447equal.
1448
1449Values of other data types, such as `list`, `dict`, and `set`, are
1450_mutable_: they may be modified by a statement such as `a[i] = 0` or
1451`items.clear()`.  Although `tuple` and `function` values are not
1452directly mutable, they may refer to mutable values indirectly, so for
1453this reason we consider them mutable too.  Starlark values of these
1454types are actually _references_ to variables.
1455
1456Copying a reference to a variable, using an assignment statement for
1457instance, creates an _alias_ for the variable, and the effects of
1458operations applied to the variable through one alias are visible
1459through all others.
1460
1461```python
1462x = []                          # x refers to a new empty list variable
1463y = x                           # y becomes an alias for x
1464x.append(1)                     # changes the variable referred to by x
1465print(y)                        # "[1]"; y observes the mutation
1466```
1467
1468Starlark uses _call-by-value_ parameter passing: in a function call,
1469argument values are assigned to function parameters as if by
1470assignment statements.  If the values are references, the caller and
1471callee may refer to the same variables, so if the called function
1472changes the variable referred to by a parameter, the effect may also
1473be observed by the caller:
1474
1475```python
1476def f(y):
1477    y.append(1)                 # changes the variable referred to by x
1478
1479x = []                          # x refers to a new empty list variable
1480f(x)                            # f's parameter y becomes an alias for x
1481print(x)                        # "[1]"; x observes the mutation
1482```
1483
1484
1485As in all imperative languages, understanding _aliasing_, the
1486relationship between reference values and the variables to which they
1487refer, is crucial to writing correct programs.
1488
1489### Freezing a value
1490
1491Starlark has a feature unusual among imperative programming languages:
1492a mutable value may be _frozen_ so that all subsequent attempts to
1493mutate it fail with a dynamic error; the value, and all other values
1494reachable from it, become _immutable_.
1495
1496Immediately after execution of a Starlark module, all values in its
1497top-level environment are frozen. Because all the global variables of
1498an initialized Starlark module are immutable, the module may be published to
1499and used by other threads in a parallel program without the need for
1500locks. For example, the Bazel build system loads and executes BUILD
1501and .bzl files in parallel, and two modules being executed
1502concurrently may freely access variables or call functions from a
1503third without the possibility of a race condition.
1504
1505### Hashing
1506
1507The `dict` and `set` data types are implemented using hash tables, so
1508only _hashable_ values are suitable as keys of a `dict` or elements of
1509a `set`. Attempting to use a non-hashable value as the key in a hash
1510table results in a dynamic error.
1511
1512The hash of a value is an unspecified integer chosen so that two equal
1513values have the same hash, in other words, `x == y => hash(x) == hash(y)`.
1514A hashable value has the same hash throughout its lifetime.
1515
1516Values of the types `NoneType`, `bool`, `int`, `float`, and `string`,
1517which are all immutable, are hashable.
1518
1519Values of mutable types such as `list`, `dict`, and `set` are not
1520hashable. These values remain unhashable even if they have become
1521immutable due to _freezing_.
1522
1523A `tuple` value is hashable only if all its elements are hashable.
1524Thus `("localhost", 80)` is hashable but `([127, 0, 0, 1], 80)` is not.
1525
1526Values of the types `function` and `builtin_function_or_method` are also hashable.
1527Although functions are not necessarily immutable, as they may be
1528closures that refer to mutable variables, instances of these types
1529are compared by reference identity (see [Comparisons](#comparisons)),
1530so their hash values are derived from their identity.
1531
1532
1533### Sequence types
1534
1535Many Starlark data types represent a _sequence_ of values: lists,
1536tuples, and sets are sequences of arbitrary values, and in many
1537contexts dictionaries act like a sequence of their keys.
1538
1539We can classify different kinds of sequence types based on the
1540operations they support.
1541Each is listed below using the name of its corresponding interface in
1542the interpreter's Go API.
1543
1544* `Iterable`: an _iterable_ value lets us process each of its elements in a fixed order.
1545  Examples: `dict`, `set`, `list`, `tuple`, but not `string`.
1546* `Sequence`: a _sequence of known length_ lets us know how many elements it
1547  contains without processing them.
1548  Examples: `dict`, `set`, `list`, `tuple`, but not `string`.
1549* `Indexable`: an _indexed_ type has a fixed length and provides efficient
1550  random access to its elements, which are identified by integer indices.
1551  Examples: `string`, `tuple`, and `list`.
1552* `SetIndexable`: a _settable indexed type_ additionally allows us to modify the
1553  element at a given integer index. Example: `list`.
1554* `Mapping`: a mapping is an association of keys to values. Example: `dict`.
1555
1556Although all of Starlark's core data types for sequences implement at
1557least the `Sequence` contract, it's possible for an application
1558that embeds the Starlark interpreter to define additional data types
1559representing sequences of unknown length that implement only the `Iterable` contract.
1560
1561Strings are not iterable, though they do support the `len(s)` and
1562`s[i]` operations. Starlark deviates from Python here to avoid a common
1563pitfall in which a string is used by mistake where a list containing a
1564single string was intended, resulting in its interpretation as a sequence
1565of bytes.
1566
1567Most Starlark operators and built-in functions that need a sequence
1568of values will accept any iterable.
1569
1570It is a dynamic error to mutate a sequence such as a list, set, or
1571dictionary while iterating over it.
1572
1573```python
1574def increment_values(dict):
1575  for k in dict:
1576    dict[k] += 1			# error: cannot insert into hash table during iteration
1577
1578dict = {"one": 1, "two": 2}
1579increment_values(dict)
1580```
1581
1582
1583### Indexing
1584
1585Many Starlark operators and functions require an index operand `i`,
1586such as `a[i]` or `list.insert(i, x)`. Others require two indices `i`
1587and `j` that indicate the start and end of a sub-sequence, such as
1588`a[i:j]`, `list.index(x, i, j)`, or `string.find(x, i, j)`.
1589All such operations follow similar conventions, described here.
1590
1591Indexing in Starlark is *zero-based*. The first element of a string
1592or list has index 0, the next 1, and so on. The last element of a
1593sequence of length `n` has index `n-1`.
1594
1595```python
1596"hello"[0]			# "h"
1597"hello"[4]			# "o"
1598"hello"[5]			# error: index out of range
1599```
1600
1601For sub-sequence operations that require two indices, the first is
1602_inclusive_ and the second _exclusive_. Thus `a[i:j]` indicates the
1603sequence starting with element `i` up to but not including element
1604`j`. The length of this sub-sequence is `j-i`. This convention is known
1605as *half-open indexing*.
1606
1607```python
1608"hello"[1:4]			# "ell"
1609```
1610
1611Either or both of the index operands may be omitted. If omitted, the
1612first is treated equivalent to 0 and the second is equivalent to the
1613length of the sequence:
1614
1615```python
1616"hello"[1:]                     # "ello"
1617"hello"[:4]                     # "hell"
1618```
1619
1620It is permissible to supply a negative integer to an indexing
1621operation. The effective index is computed from the supplied value by
1622the following two-step procedure. First, if the value is negative, the
1623length of the sequence is added to it. This provides a convenient way
1624to address the final elements of the sequence:
1625
1626```python
1627"hello"[-1]                     # "o",  like "hello"[4]
1628"hello"[-3:-1]                  # "ll", like "hello"[2:4]
1629```
1630
1631Second, for sub-sequence operations, if the value is still negative, it
1632is replaced by zero, or if it is greater than the length `n` of the
1633sequence, it is replaced by `n`. In effect, the index is "truncated" to
1634the nearest value in the range `[0:n]`.
1635
1636```python
1637"hello"[-1000:+1000]		# "hello"
1638```
1639
1640This truncation step does not apply to indices of individual elements:
1641
1642```python
1643"hello"[-6]		# error: index out of range
1644"hello"[-5]		# "h"
1645"hello"[4]		# "o"
1646"hello"[5]		# error: index out of range
1647```
1648
1649
1650## Expressions
1651
1652An expression specifies the computation of a value.
1653
1654The Starlark grammar defines several categories of expression.
1655An _operand_ is an expression consisting of a single token (such as an
1656identifier or a literal), or a bracketed expression.
1657Operands are self-delimiting.
1658An operand may be followed by any number of dot, call, or slice
1659suffixes, to form a _primary_ expression.
1660In some places in the Starlark grammar where an expression is expected,
1661it is legal to provide a comma-separated list of expressions denoting
1662a tuple.
1663The grammar uses `Expression` where a multiple-component expression is allowed,
1664and `Test` where it accepts an expression of only a single component.
1665
1666```grammar {.good}
1667Expression = Test {',' Test} .
1668
1669Test = LambdaExpr | IfExpr | PrimaryExpr | UnaryExpr | BinaryExpr .
1670
1671PrimaryExpr = Operand
1672            | PrimaryExpr DotSuffix
1673            | PrimaryExpr CallSuffix
1674            | PrimaryExpr SliceSuffix
1675            .
1676
1677Operand = identifier
1678        | int | float | string
1679        | ListExpr | ListComp
1680        | DictExpr | DictComp
1681        | '(' [Expression] [,] ')'
1682        | ('-' | '+') PrimaryExpr
1683        .
1684
1685DotSuffix   = '.' identifier .
1686CallSuffix  = '(' [Arguments [',']] ')' .
1687SliceSuffix = '[' [Expression] [':' Test [':' Test]] ']' .
1688```
1689
1690TODO: resolve position of +x, -x, and 'not x' in grammar: Operand or UnaryExpr?
1691
1692### Identifiers
1693
1694```grammar {.good} {.good}
1695Primary = identifier
1696```
1697
1698An identifier is a name that identifies a value.
1699
1700Lookup of locals and globals may fail if not yet defined.
1701
1702### Literals
1703
1704Starlark supports literals of three different kinds:
1705
1706```grammar {.good}
1707Primary = int | float | string
1708```
1709
1710Evaluation of a literal yields a value of the given type (string, int,
1711or float) with the given value.
1712See [Literals](#lexical-elements) for details.
1713
1714### Parenthesized expressions
1715
1716```grammar {.good}
1717Primary = '(' [Expression] ')'
1718```
1719
1720A single expression enclosed in parentheses yields the result of that expression.
1721Explicit parentheses may be used for clarity,
1722or to override the default association of subexpressions.
1723
1724```python
17251 + 2 * 3 + 4                   # 11
1726(1 + 2) * (3 + 4)               # 21
1727```
1728
1729If the parentheses are empty, or contain a single expression followed
1730by a comma, or contain two or more expressions, the expression yields a tuple.
1731
1732```python
1733()                              # (), the empty tuple
1734(1,)                            # (1,), a tuple of length 1
1735(1, 2)                          # (1, 2), a 2-tuple or pair
1736(1, 2, 3)                       # (1, 2, 3), a 3-tuple or triple
1737```
1738
1739In some contexts, such as a `return` or assignment statement or the
1740operand of a `for` statement, a tuple may be expressed without
1741parentheses.
1742
1743```python
1744x, y = 1, 2
1745
1746return 1, 2
1747
1748for x in 1, 2:
1749   print(x)
1750```
1751
1752Starlark (like Python 3) does not accept an unparenthesized tuple
1753expression as the operand of a list comprehension:
1754
1755```python
1756[2*x for x in 1, 2, 3]	       	# parse error: unexpected ','
1757```
1758
1759### Dictionary expressions
1760
1761A dictionary expression is a comma-separated list of colon-separated
1762key/value expression pairs, enclosed in curly brackets, and it yields
1763a new dictionary object.
1764An optional comma may follow the final pair.
1765
1766```grammar {.good}
1767DictExpr = '{' [Entries [',']] '}' .
1768Entries  = Entry {',' Entry} .
1769Entry    = Test ':' Test .
1770```
1771
1772Examples:
1773
1774
1775```python
1776{}
1777{"one": 1}
1778{"one": 1, "two": 2,}
1779```
1780
1781The key and value expressions are evaluated in left-to-right order.
1782Evaluation fails if the same key is used multiple times.
1783
1784Only [hashable](#hashing) values may be used as the keys of a dictionary.
1785This includes all built-in types except dictionaries, sets, and lists;
1786a tuple is hashable only if its elements are hashable.
1787
1788
1789### List expressions
1790
1791A list expression is a comma-separated list of element expressions,
1792enclosed in square brackets, and it yields a new list object.
1793An optional comma may follow the last element expression.
1794
1795```grammar {.good}
1796ListExpr = '[' [Expression [',']] ']' .
1797```
1798
1799Element expressions are evaluated in left-to-right order.
1800
1801Examples:
1802
1803```python
1804[]                      # [], empty list
1805[1]                     # [1], a 1-element list
1806[1, 2, 3,]              # [1, 2, 3], a 3-element list
1807```
1808
1809### Unary operators
1810
1811There are three unary operators, all appearing before their operand:
1812`+`, `-`, `~`, and `not`.
1813
1814```grammar {.good}
1815UnaryExpr = '+' PrimaryExpr
1816          | '-' PrimaryExpr
1817          | '~' PrimaryExpr
1818          | 'not' Test
1819          .
1820```
1821
1822```text
1823+ number        unary positive          (int, float)
1824- number        unary negation          (int, float)
1825~ number        unary bitwise inversion (int)
1826not x           logical negation        (any type)
1827```
1828
1829The `+` and `-` operators may be applied to any number
1830(`int` or `float`) and return the number unchanged.
1831Unary `+` is never necessary in a correct program,
1832but may serve as an assertion that its operand is a number,
1833or as documentation.
1834
1835```python
1836if x > 0:
1837	return +1
1838else if x < 0:
1839	return -1
1840else:
1841	return 0
1842```
1843
1844The `not` operator returns the negation of the truth value of its
1845operand.
1846
1847```python
1848not True                        # False
1849not False                       # True
1850not [1, 2, 3]                   # False
1851not ""                          # True
1852not 0                           # True
1853```
1854
1855The `~` operator yields the bitwise inversion of its integer argument.
1856The bitwise inversion of x is defined as -(x+1).
1857
1858```python
1859~1                              # -2
1860~-1                             # 0
1861~0                              # -1
1862```
1863
1864
1865### Binary operators
1866
1867Starlark has the following binary operators, arranged in order of increasing precedence:
1868
1869```text
1870or
1871and
1872==   !=   <    >   <=   >=   in   not in
1873|
1874^
1875&
1876<<   >>
1877-    +
1878*    /    //   %
1879```
1880
1881Comparison operators, `in`, and `not in` are non-associative,
1882so the parser will not accept `0 <= i < n`.
1883All other binary operators of equal precedence associate to the left.
1884
1885```grammar {.good}
1886BinaryExpr = Test {Binop Test} .
1887
1888Binop = 'or'
1889      | 'and'
1890      | '==' | '!=' | '<' | '>' | '<=' | '>=' | 'in' | 'not' 'in'
1891      | '|'
1892      | '^'
1893      | '&'
1894      | '-' | '+'
1895      | '*' | '%' | '/' | '//'
1896      | '<<' | '>>'
1897      .
1898```
1899
1900#### `or` and `and`
1901
1902The `or` and `and` operators yield, respectively, the logical disjunction and
1903conjunction of their arguments, which need not be Booleans.
1904The expression `x or y` yields the value of `x` if its truth value is `True`,
1905or the value of `y` otherwise.
1906
1907```starlark
1908False or False		# False
1909False or True		# True
1910True  or False		# True
1911True  or True		# True
1912
19130 or "hello"		# "hello"
19141 or "hello"		# 1
1915```
1916
1917Similarly, `x and y` yields the value of `x` if its truth value is
1918`False`, or the value of `y` otherwise.
1919
1920```starlark
1921False and False		# False
1922False and True		# False
1923True  and False		# False
1924True  and True		# True
1925
19260 and "hello"		# 0
19271 and "hello"		# "hello"
1928```
1929
1930These operators use "short circuit" evaluation, so the second
1931expression is not evaluated if the value of the first expression has
1932already determined the result, allowing constructions like these:
1933
1934```python
1935len(x) > 0 and x[0] == 1		# x[0] is not evaluated if x is empty
1936x and x[0] == 1
1937len(x) == 0 or x[0] == ""
1938not x or not x[0]
1939```
1940
1941#### Comparisons
1942
1943The `==` operator reports whether its operands are equal; the `!=`
1944operator is its negation.
1945
1946The operators `<`, `>`, `<=`, and `>=` perform an ordered comparison
1947of their operands.  It is an error to apply these operators to
1948operands of unequal type, unless one of the operands is an `int` and
1949the other is a `float`.  Of the built-in types, only the following
1950support ordered comparison, using the ordering relation shown:
1951
1952```shell
1953NoneType        # None <= None
1954bool            # False < True
1955int             # mathematical
1956float           # as defined by IEEE 754
1957string          # lexicographical
1958tuple           # lexicographical
1959list            # lexicographical
1960```
1961
1962Comparison of floating point values follows the IEEE 754 standard,
1963which breaks several mathematical identities.  For example, if `x` is
1964a `NaN` value, the comparisons `x < y`, `x == y`, and `x > y` all
1965yield false for all values of `y`.
1966
1967Applications may define additional types that support ordered
1968comparison.
1969
1970The remaining built-in types support only equality comparisons.
1971Values of type `dict` or `set` compare equal if their elements compare
1972equal, and values of type `function` or `builtin_function_or_method` are equal only to
1973themselves.
1974
1975```shell
1976dict                            # equal contents
1977set                             # equal contents
1978function                        # identity
1979builtin_function_or_method      # identity
1980```
1981
1982#### Arithmetic operations
1983
1984The following table summarizes the binary arithmetic operations
1985available for built-in types:
1986
1987```shell
1988Arithmetic (int or float; result has type float unless both operands have type int)
1989   number + number              # addition
1990   number - number              # subtraction
1991   number * number              # multiplication
1992   number / number              # real division  (result is always a float)
1993   number // number             # floored division
1994   number % number              # remainder of floored division
1995   number ^ number              # bitwise XOR
1996   number << number             # bitwise left shift
1997   number >> number             # bitwise right shift
1998
1999Concatenation
2000   string + string
2001     list + list
2002    tuple + tuple
2003
2004Repetition (string/list/tuple)
2005      int * sequence
2006 sequence * int
2007
2008String interpolation
2009   string % any                 # see String Interpolation
2010
2011Sets
2012      int | int                 # bitwise union (OR)
2013      set | set                 # set union
2014      int & int                 # bitwise intersection (AND)
2015      set & set                 # set intersection
2016      set ^ set                 # set symmetric difference
2017```
2018
2019The operands of the arithmetic operators `+`, `-`, `*`, `//`, and
2020`%` must both be numbers (`int` or `float`) but need not have the same type.
2021The type of the result has type `int` only if both operands have that type.
2022The result of real division `/` always has type `float`.
2023
2024The `+` operator may be applied to non-numeric operands of the same
2025type, such as two lists, two tuples, or two strings, in which case it
2026computes the concatenation of the two operands and yields a new value of
2027the same type.
2028
2029```python
2030"Hello, " + "world"		# "Hello, world"
2031(1, 2) + (3, 4)			# (1, 2, 3, 4)
2032[1, 2] + [3, 4]			# [1, 2, 3, 4]
2033```
2034
2035The `*` operator may be applied to an integer _n_ and a value of type
2036`string`, `list`, or `tuple`, in which case it yields a new value
2037of the same sequence type consisting of _n_ repetitions of the original sequence.
2038The order of the operands is immaterial.
2039Negative values of _n_ behave like zero.
2040
2041```python
2042'mur' * 2               # 'murmur'
20433 * range(3)            # [0, 1, 2, 0, 1, 2, 0, 1, 2]
2044```
2045
2046Applications may define additional types that support any subset of
2047these operators.
2048
2049The `&` operator requires two operands of the same type, either `int` or `set`.
2050For integers, it yields the bitwise intersection (AND) of its operands.
2051For sets, it yields a new set containing the intersection of the
2052elements of the operand sets, preserving the element order of the left
2053operand.
2054
2055The `|` operator likewise computes bitwise or set unions.
2056The result of `set | set` is a new set whose elements are the
2057union of the operands, preserving the order of the elements of the
2058operands, left before right.
2059
2060The `^` operator accepts operands of either `int` or `set` type.
2061For integers, it yields the bitwise XOR (exclusive OR) of its operands.
2062For sets, it yields a new set containing elements of either first or second
2063operand but not both (symmetric difference).
2064
2065The `<<` and `>>` operators require operands of `int` type both. They shift
2066the first operand to the left or right by the number of bits given by the
2067second operand. It is a dynamic error if the second operand is negative.
2068Implementations may impose a limit on the second operand of a left shift.
2069
2070```python
20710x12345678 & 0xFF               # 0x00000078
20720x12345678 | 0xFF               # 0x123456FF
20730b01011101 ^ 0b110101101        # 0b111110000
20740b01011101 >> 2                 # 0b010111
20750b01011101 << 2                 # 0b0101110100
2076
2077set([1, 2]) & set([2, 3])       # set([2])
2078set([1, 2]) | set([2, 3])       # set([1, 2, 3])
2079set([1, 2]) ^ set([2, 3])       # set([1, 3])
2080```
2081
2082<b>Implementation note:</b>
2083The Go implementation of Starlark requires the `-set` flag to
2084enable support for sets.
2085The Java implementation does not support sets.
2086
2087
2088#### Membership tests
2089
2090```text
2091      any in     sequence		(list, tuple, dict, set, string)
2092      any not in sequence
2093```
2094
2095The `in` operator reports whether its first operand is a member of its
2096second operand, which must be a list, tuple, dict, set, or string.
2097The `not in` operator is its negation.
2098Both return a Boolean.
2099
2100The meaning of membership varies by the type of the second operand:
2101the members of a list, tuple, or set are its elements;
2102the members of a dict are its keys;
2103the members of a string are all its substrings.
2104
2105```python
21061 in [1, 2, 3]                  # True
21074 in (1, 2, 3)                  # False
21084 not in set([1, 2, 3])         # True
2109
2110d = {"one": 1, "two": 2}
2111"one" in d                      # True
2112"three" in d                    # False
21131 in d                          # False
2114[] in d				# False
2115
2116"nasty" in "dynasty"            # True
2117"a" in "banana"                 # True
2118"f" not in "way"                # True
2119```
2120
2121#### String interpolation
2122
2123The expression `format % args` performs _string interpolation_, a
2124simple form of template expansion.
2125The `format` string is interpreted as a sequence of literal portions
2126and _conversions_.
2127Each conversion, which starts with a `%` character, is replaced by its
2128corresponding value from `args`.
2129The characters following `%` in each conversion determine which
2130argument it uses and how to convert it to a string.
2131
2132Each `%` character marks the start of a conversion specifier, unless
2133it is immediately followed by another `%`, in which case both
2134characters together denote a literal percent sign.
2135
2136If the `"%"` is immediately followed by `"(key)"`, the parenthesized
2137substring specifies the key of the `args` dictionary whose
2138corresponding value is the operand to convert.
2139Otherwise, the conversion's operand is the next element of `args`,
2140which must be a tuple with exactly one component per conversion,
2141unless the format string contains only a single conversion, in which
2142case `args` itself is its operand.
2143
2144Starlark does not support the flag, width, and padding specifiers
2145supported by Python's `%` and other variants of C's `printf`.
2146
2147After the optional `(key)` comes a single letter indicating what
2148operand types are valid and how to convert the operand `x` to a string:
2149
2150```text
2151%       none            literal percent sign
2152s       any             as if by str(x)
2153r       any             as if by repr(x)
2154d       number          signed integer decimal
2155i       number          signed integer decimal
2156o       number          signed octal
2157x       number          signed hexadecimal, lowercase
2158X       number          signed hexadecimal, uppercase
2159e       number          float exponential format, lowercase
2160E       number          float exponential format, uppercase
2161f       number          float decimal format, lowercase
2162F       number          float decimal format, uppercase
2163g       number          like %e for large exponents, %f otherwise
2164G       number          like %E for large exponents, %F otherwise
2165c       string          x (string must encode a single Unicode code point)
2166        int             as if by chr(x)
2167```
2168
2169It is an error if the argument does not have the type required by the
2170conversion specifier.  A Boolean argument is not considered a number.
2171
2172Examples:
2173
2174```python
2175"Hello %s, your score is %d" % ("Bob", 75)      # "Hello Bob, your score is 75"
2176
2177"%d %o %x %c" % (65, 65, 65, 65)                # "65 101 41 A" (decimal, octal, hexadecimal, Unicode)
2178
2179"%(greeting)s, %(audience)s" % dict(            # "Hello, world"
2180  greeting="Hello",
2181  audience="world",
2182)
2183
2184"rate = %g%% APR" % 3.5                         # "rate = 3.5% APR"
2185```
2186
2187One subtlety: to use a tuple as the operand of a conversion in format
2188string containing only a single conversion, you must wrap the tuple in
2189a singleton tuple:
2190
2191```python
2192"coordinates=%s" % (40.741491, -74.003680)	# error: too many arguments for format string
2193"coordinates=%s" % ((40.741491, -74.003680),)	# "coordinates=(40.741491, -74.003680)"
2194```
2195
2196TODO: specify `%e` and `%f` more precisely.
2197
2198### Conditional expressions
2199
2200A conditional expression has the form `a if cond else b`.
2201It first evaluates the condition `cond`.
2202If it's true, it evaluates `a` and yields its value;
2203otherwise it yields the value of `b`.
2204
2205```grammar {.good}
2206IfExpr = Test 'if' Test 'else' Test .
2207```
2208
2209Example:
2210
2211```python
2212"yes" if enabled else "no"
2213```
2214
2215### Comprehensions
2216
2217A comprehension constructs new list or dictionary value by looping
2218over one or more iterables and evaluating a _body_ expression that produces
2219successive elements of the result.
2220
2221A list comprehension consists of a single expression followed by one
2222or more _clauses_, the first of which must be a `for` clause.
2223Each `for` clause resembles a `for` statement, and specifies an
2224iterable operand and a set of variables to be assigned by successive
2225values of the iterable.
2226An `if` cause resembles an `if` statement, and specifies a condition
2227that must be met for the body expression to be evaluated.
2228A sequence of `for` and `if` clauses acts like a nested sequence of
2229`for` and `if` statements.
2230
2231```grammar {.good}
2232ListComp = '[' Test {CompClause} ']'.
2233DictComp = '{' Entry {CompClause} '}' .
2234
2235CompClause = 'for' LoopVariables 'in' Test
2236           | 'if' Test .
2237
2238LoopVariables = PrimaryExpr {',' PrimaryExpr} .
2239```
2240
2241Examples:
2242
2243```python
2244[x*x for x in range(5)]                 # [0, 1, 4, 9, 16]
2245[x*x for x in range(5) if x%2 == 0]     # [0, 4, 16]
2246[(x, y) for x in range(5)
2247        if x%2 == 0
2248        for y in range(5)
2249        if y > x]                       # [(0, 1), (0, 2), (0, 3), (0, 4), (2, 3), (2, 4)]
2250```
2251
2252A dict comprehension resembles a list comprehension, but its body is a
2253pair of expressions, `key: value`, separated by a colon,
2254and its result is a dictionary containing the key/value pairs
2255for which the body expression was evaluated.
2256Evaluation fails if the value of any key is unhashable.
2257
2258As with a `for` loop, the loop variables may exploit compound
2259assignment:
2260
2261```python
2262[x*y+z for (x, y), z in [((2, 3), 5), (("o", 2), "!")]]         # [11, 'oo!']
2263```
2264
2265Starlark, following Python 3, does not accept an unparenthesized
2266tuple or lambda expression as the operand of a `for` clause:
2267
2268```python
2269[x*x for x in 1, 2, 3]		# parse error: unexpected comma
2270[x*x for x in lambda: 0]	# parse error: unexpected lambda
2271```
2272
2273Comprehensions in Starlark, again following Python 3, define a new lexical
2274block, so assignments to loop variables have no effect on variables of
2275the same name in an enclosing block:
2276
2277```python
2278x = 1
2279_ = [x for x in [2]]            # new variable x is local to the comprehension
2280print(x)                        # 1
2281```
2282
2283The operand of a comprehension's first clause (always a `for`) is
2284resolved in the lexical block enclosing the comprehension.
2285In the examples below, identifiers referring to the outer variable
2286named `x` have been distinguished by subscript.
2287
2288```python
2289x₀ = (1, 2, 3)
2290[x*x for x in x₀]               # [1, 4, 9]
2291[x*x for x in x₀ if x%2 == 0]   # [4]
2292```
2293
2294All subsequent `for` and `if` expressions are resolved within the
2295comprehension's lexical block, as in this rather obscure example:
2296
2297```python
2298x₀ = ([1, 2], [3, 4], [5, 6])
2299[x*x for x in x₀ for x in x if x%2 == 0]     # [4, 16, 36]
2300```
2301
2302which would be more clearly rewritten as:
2303
2304```python
2305x = ([1, 2], [3, 4], [5, 6])
2306[z*z for y in x for z in y if z%2 == 0]     # [4, 16, 36]
2307```
2308
2309
2310### Function and method calls
2311
2312```grammar {.good}
2313CallSuffix = '(' [Arguments [',']] ')' .
2314
2315Arguments = Argument {',' Argument} .
2316Argument  = Test | identifier '=' Test | '*' Test | '**' Test .
2317```
2318
2319A value `f` of type `function` or `builtin_function_or_method` may be called using the expression `f(...)`.
2320Applications may define additional types whose values may be called in the same way.
2321
2322A method call such as `filename.endswith(".star")` is the composition
2323of two operations, `m = filename.endswith` and `m(".star")`.
2324The first, a dot operation, yields a _bound method_, a function value
2325that pairs a receiver value (the `filename` string) with a choice of
2326method ([string·endswith](#string·endswith)).
2327
2328Only built-in or application-defined types may have methods.
2329
2330See [Functions](#functions) for an explanation of function parameter passing.
2331
2332### Dot expressions
2333
2334A dot expression `x.f` selects the attribute `f` (a field or method)
2335of the value `x`.
2336
2337Fields are possessed by none of the main Starlark [data types](#data-types),
2338but some application-defined types have them.
2339Methods belong to the built-in types `string`, `list`, `dict`, and
2340`set`, and to many application-defined types.
2341
2342```grammar {.good}
2343DotSuffix = '.' identifier .
2344```
2345
2346A dot expression fails if the value does not have an attribute of the
2347specified name.
2348
2349Use the built-in function `hasattr(x, "f")` to ascertain whether a
2350value has a specific attribute, or `dir(x)` to enumerate all its
2351attributes.  The `getattr(x, "f")` function can be used to select an
2352attribute when the name `"f"` is not known statically.
2353
2354A dot expression that selects a method typically appears within a call
2355expression, as in these examples:
2356
2357```python
2358["able", "baker", "charlie"].index("baker")     # 1
2359"banana".count("a")                             # 3
2360"banana".reverse()                              # error: string has no .reverse field or method
2361```
2362
2363But when not called immediately, the dot expression evaluates to a
2364_bound method_, that is, a method coupled to a specific receiver
2365value.  A bound method can be called like an ordinary function,
2366without a receiver argument:
2367
2368```python
2369f = "banana".count
2370f                                               # <built-in method count of string value>
2371f("a")                                          # 3
2372f("n")                                          # 2
2373```
2374
2375### Index expressions
2376
2377An index expression `a[i]` yields the `i`th element of an _indexable_
2378type such as a string, tuple, or list.  The index `i` must be an `int`
2379value in the range -`n` ≤ `i` < `n`, where `n` is `len(a)`; any other
2380index results in an error.
2381
2382```grammar {.good}
2383SliceSuffix = '[' [Expression] [':' Test [':' Test]] ']' .
2384```
2385
2386A valid negative index `i` behaves like the non-negative index `n+i`,
2387allowing for convenient indexing relative to the end of the
2388sequence.
2389
2390```python
2391"abc"[0]                        # "a"
2392"abc"[1]                        # "b"
2393"abc"[-1]                       # "c"
2394
2395("zero", "one", "two")[0]       # "zero"
2396("zero", "one", "two")[1]       # "one"
2397("zero", "one", "two")[-1]      # "two"
2398```
2399
2400An index expression `d[key]` may also be applied to a dictionary `d`,
2401to obtain the value associated with the specified key.  It is an error
2402if the dictionary contains no such key.
2403
2404An index expression appearing on the left side of an assignment causes
2405the specified list or dictionary element to be updated:
2406
2407```starlark
2408a = range(3)            # a == [0, 1, 2]
2409a[2] = 7                # a == [0, 1, 7]
2410
2411coins["suzie b"] = 100
2412```
2413
2414It is a dynamic error to attempt to update an element of an immutable
2415type, such as a tuple or string, or a frozen value of a mutable type.
2416
2417### Slice expressions
2418
2419A slice expression `a[start:stop:stride]` yields a new value containing a
2420sub-sequence of `a`, which must be a string, tuple, or list.
2421
2422```grammar {.good}
2423SliceSuffix = '[' [Expression] [':' Test [':' Test]] ']' .
2424```
2425
2426Each of the `start`, `stop`, and `stride` operands is optional;
2427if present, and not `None`, each must be an integer.
2428The `stride` value defaults to 1.
2429If the stride is not specified, the colon preceding it may be omitted too.
2430It is an error to specify a stride of zero.
2431
2432Conceptually, these operands specify a sequence of values `i` starting
2433at `start` and successively adding `stride` until `i` reaches or
2434passes `stop`. The result consists of the concatenation of values of
2435`a[i]` for which `i` is valid.`
2436
2437The effective start and stop indices are computed from the three
2438operands as follows.  Let `n` be the length of the sequence.
2439
2440<b>If the stride is positive:</b>
2441If the `start` operand was omitted, it defaults to -infinity.
2442If the `end` operand was omitted, it defaults to +infinity.
2443For either operand, if a negative value was supplied, `n` is added to it.
2444The `start` and `end` values are then "clamped" to the
2445nearest value in the range 0 to `n`, inclusive.
2446
2447<b>If the stride is negative:</b>
2448If the `start` operand was omitted, it defaults to +infinity.
2449If the `end` operand was omitted, it defaults to -infinity.
2450For either operand, if a negative value was supplied, `n` is added to it.
2451The `start` and `end` values are then "clamped" to the
2452nearest value in the range -1 to `n`-1, inclusive.
2453
2454```python
2455"abc"[1:]               # "bc"  (remove first element)
2456"abc"[:-1]              # "ab"  (remove last element)
2457"abc"[1:-1]             # "b"   (remove first and last element)
2458"banana"[1::2]          # "aaa" (select alternate elements starting at index 1)
2459"banana"[4::-2]         # "nnb" (select alternate elements in reverse, starting at index 4)
2460```
2461
2462Unlike Python, Starlark does not allow a slice expression on the left
2463side of an assignment.
2464
2465Slicing a tuple or string may be more efficient than slicing a list
2466because tuples and strings are immutable, so the result of the
2467operation can share the underlying representation of the original
2468operand (when the stride is 1). By contrast, slicing a list requires
2469the creation of a new list and copying of the necessary elements.
2470
2471<!-- TODO tighten up this section -->
2472
2473### Lambda expressions
2474
2475A `lambda` expression yields a new function value.
2476
2477```grammar {.good}
2478LambdaExpr = 'lambda' [Parameters] ':' Test .
2479
2480Parameters = Parameter {',' Parameter} .
2481Parameter  = identifier
2482           | identifier '=' Test
2483           | '*'
2484           | '*' identifier
2485           | '**' identifier
2486           .
2487```
2488
2489Syntactically, a lambda expression consists of the keyword `lambda`,
2490followed by a parameter list like that of a `def` statement but
2491unparenthesized, then a colon `:`, and a single expression, the
2492_function body_.
2493
2494Example:
2495
2496```python
2497def map(f, list):
2498    return [f(x) for x in list]
2499
2500map(lambda x: 2*x, range(3))    # [2, 4, 6]
2501```
2502
2503As with functions created by a `def` statement, a lambda function
2504captures the syntax of its body, the default values of any optional
2505parameters, the value of each free variable appearing in its body, and
2506the global dictionary of the current module.
2507
2508The name of a function created by a lambda expression is `"lambda"`.
2509
2510The two statements below are essentially equivalent, but the
2511function created by the `def` statement is named `twice` and the
2512function created by the lambda expression is named `lambda`.
2513
2514```python
2515def twice(x):
2516   return x * 2
2517
2518twice = lambda x: x * 2
2519```
2520
2521## Statements
2522
2523```grammar {.good}
2524Statement  = DefStmt | IfStmt | ForStmt | SimpleStmt .
2525SimpleStmt = SmallStmt {';' SmallStmt} [';'] '\n' .
2526SmallStmt  = ReturnStmt
2527           | BreakStmt | ContinueStmt | PassStmt
2528           | AssignStmt
2529           | ExprStmt
2530           | LoadStmt
2531           .
2532```
2533
2534### Pass statements
2535
2536A `pass` statement does nothing.  Use a `pass` statement when the
2537syntax requires a statement but no behavior is required, such as the
2538body of a function that does nothing.
2539
2540```grammar {.good}
2541PassStmt = 'pass' .
2542```
2543
2544Example:
2545
2546```python
2547def noop():
2548   pass
2549
2550def list_to_dict(items):
2551  # Convert list of tuples to dict
2552  m = {}
2553  for k, m[k] in items:
2554    pass
2555  return m
2556```
2557
2558### Assignments
2559
2560An assignment statement has the form `lhs = rhs`.  It evaluates the
2561expression on the right-hand side then assigns its value (or values) to
2562the variable (or variables) on the left-hand side.
2563
2564```grammar {.good}
2565AssignStmt = Expression '=' Expression .
2566```
2567
2568The expression on the left-hand side is called a _target_.  The
2569simplest target is the name of a variable, but a target may also have
2570the form of an index expression, to update the element of a list or
2571dictionary, or a dot expression, to update the field of an object:
2572
2573```python
2574k = 1
2575a[i] = v
2576m.f = ""
2577```
2578
2579Compound targets may consist of a comma-separated list of
2580subtargets, optionally surrounded by parentheses or square brackets,
2581and targets may be nested arbitarily in this way.
2582An assignment to a compound target checks that the right-hand value is a
2583sequence with the same number of elements as the target.
2584Each element of the sequence is then assigned to the corresponding
2585element of the target, recursively applying the same logic.
2586
2587```python
2588pi, e = 3.141, 2.718
2589(x, y) = f()
2590[zero, one, two] = range(3)
2591
2592[(a, b), (c, d)] = {"a": "b", "c": "d"}.items()
2593a, b = {"a": 1, "b": 2}
2594```
2595
2596The same process for assigning a value to a target expression is used
2597in `for` loops and in comprehensions.
2598
2599
2600### Augmented assignments
2601
2602An augmented assignment, which has the form `lhs op= rhs` updates the
2603variable `lhs` by applying a binary arithmetic operator `op` (one of
2604`+`, `-`, `*`, `/`, `//`, `%`, `&`, `|`, `^`, `<<`, `>>`) to the previous
2605value of `lhs` and the value of `rhs`.
2606
2607```grammar {.good}
2608AssignStmt = Expression ('+=' | '-=' | '*=' | '/=' | '//=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=') Expression .
2609```
2610
2611The left-hand side must be a simple target:
2612a name, an index expression, or a dot expression.
2613
2614```python
2615x -= 1
2616x.filename += ".star"
2617a[index()] *= 2
2618```
2619
2620Any subexpressions in the target on the left-hand side are evaluated
2621exactly once, before the evaluation of `rhs`.
2622The first two assignments above are thus equivalent to:
2623
2624```python
2625x = x - 1
2626x.filename = x.filename + ".star"
2627```
2628
2629and the third assignment is similar in effect to the following two
2630statements but does not declare a new temporary variable `i`:
2631
2632```python
2633i = index()
2634a[i] = a[i] * 2
2635```
2636
2637### Function definitions
2638
2639A `def` statement creates a named function and assigns it to a variable.
2640
2641```grammar {.good}
2642DefStmt = 'def' identifier '(' [Parameters [',']] ')' ':' Suite .
2643```
2644
2645Example:
2646
2647```python
2648def twice(x):
2649    return x * 2
2650
2651str(twice)              # "<function twice>"
2652twice(2)                # 4
2653twice("two")            # "twotwo"
2654```
2655
2656The function's name is preceded by the `def` keyword and followed by
2657the parameter list (which is enclosed in parentheses), a colon, and
2658then an indented block of statements which form the body of the function.
2659
2660The parameter list is a comma-separated list whose elements are of
2661several kinds.  First come zero or more required parameters, which are
2662simple identifiers; all calls must provide an argument value for these parameters.
2663
2664The required parameters are followed by zero or more optional
2665parameters, of the form `name=expression`.  The expression specifies
2666the default value for the parameter for use in calls that do not
2667provide an argument value for it.
2668
2669The required parameters are optionally followed by a single parameter
2670name preceded by a `*`.  This is the called the _varargs_ parameter,
2671and it accumulates surplus positional arguments specified by a call.
2672It is conventionally named `*args`.
2673
2674The varargs parameter may be followed by zero or more
2675parameters, again of the forms `name` or `name=expression`,
2676but these parameters differ from earlier ones in that they are
2677_keyword-only_: if a call provides their values, it must do so as
2678keyword arguments, not positional ones.
2679
2680```python
2681def f(a, *, b=2, c):
2682  print(a, b, c)
2683
2684f(1)                    # error: function f missing 1 argument (c)
2685f(1, 3)                 # error: function f accepts 1 positional argument (2 given)
2686f(1, c=3)               # "1 2 3"
2687
2688def g(a, *args, b=2, c):
2689  print(a, b, c, args)
2690
2691g(1, 3)                 # error: function g missing 1 argument (c)
2692g(1, 4, c=3)            # "1 2 3 (4,)"
2693
2694```
2695
2696A non-variadic function may also declare keyword-only parameters,
2697by using a bare `*` in place of the `*args` parameter.
2698This form does not declare a parameter but marks the boundary
2699between the earlier parameters and the keyword-only parameters.
2700This form must be followed by at least one optional parameter.
2701
2702Finally, there may be an optional parameter name preceded by `**`.
2703This is called the _keyword arguments_ parameter, and accumulates in a
2704dictionary any surplus `name=value` arguments that do not match a
2705prior parameter. It is conventionally named `**kwargs`.
2706
2707The final parameter may be followed by a trailing comma.
2708
2709Here are some example parameter lists:
2710
2711```python
2712def f(): pass
2713def f(a, b, c): pass
2714def f(a, b, c=1): pass
2715def f(a, b, c=1, *args): pass
2716def f(a, b, c=1, *args, **kwargs): pass
2717def f(**kwargs): pass
2718def f(a, b, c=1, *, d=1): pass
2719
2720def f(
2721  a,
2722  *args,
2723  **kwargs,
2724)
2725```
2726
2727Execution of a `def` statement creates a new function object.  The
2728function object contains: the syntax of the function body; the default
2729value for each optional parameter; the value of each free variable
2730referenced within the function body; and the global dictionary of the
2731current module.
2732
2733<!-- this is too implementation-oriented; it's not a spec. -->
2734
2735
2736### Return statements
2737
2738A `return` statement ends the execution of a function and returns a
2739value to the caller of the function.
2740
2741```grammar {.good}
2742ReturnStmt = 'return' [Expression] .
2743```
2744
2745A return statement may have zero, one, or more
2746result expressions separated by commas.
2747With no expressions, the function has the result `None`.
2748With a single expression, the function's result is the value of that expression.
2749With multiple expressions, the function's result is a tuple.
2750
2751```python
2752return                  # returns None
2753return 1                # returns 1
2754return 1, 2             # returns (1, 2)
2755```
2756
2757### Expression statements
2758
2759An expression statement evaluates an expression and discards its result.
2760
2761```grammar {.good}
2762ExprStmt = Expression .
2763```
2764
2765Any expression may be used as a statement, but an expression statement is
2766most often used to call a function for its side effects.
2767
2768```python
2769list.append(1)
2770```
2771
2772### If statements
2773
2774An `if` statement evaluates an expression (the _condition_), then, if
2775the truth value of the condition is `True`, executes a list of
2776statements.
2777
2778```grammar {.good}
2779IfStmt = 'if' Test ':' Suite {'elif' Test ':' Suite} ['else' ':' Suite] .
2780```
2781
2782Example:
2783
2784```python
2785if score >= 100:
2786    print("You win!")
2787    return
2788```
2789
2790An `if` statement may have an `else` block defining a second list of
2791statements to be executed if the condition is false.
2792
2793```python
2794if score >= 100:
2795        print("You win!")
2796        return
2797else:
2798        print("Keep trying...")
2799        continue
2800```
2801
2802It is common for the `else` block to contain another `if` statement.
2803To avoid increasing the nesting depth unnecessarily, the `else` and
2804following `if` may be combined as `elif`:
2805
2806```python
2807if x > 0:
2808        result = +1
2809elif x < 0:
2810        result = -1
2811else:
2812        result = 0
2813```
2814
2815An `if` statement is permitted only within a function definition.
2816An `if` statement at top level results in a static error.
2817
2818<b>Implementation note:</b>
2819The Go implementation of Starlark permits `if`-statements to appear at top level
2820if the `-globalreassign` flag is enabled.
2821
2822
2823### While loops
2824
2825A `while` loop evaluates an expression (the _condition_) and if the truth
2826value of the condition is `True`, it executes a list of statement and repeats
2827the process until the truth value of the condition becomes `False`.
2828
2829```grammar {.good}
2830WhileStmt = 'while' Test ':' Suite .
2831```
2832
2833Example:
2834
2835```python
2836while n > 0:
2837    r = r + n
2838    n = n - 1
2839```
2840
2841A `while` statement is permitted only within a function definition.
2842A `while` statement at top level results in a static error.
2843
2844<b>Implementation note:</b>
2845The Go implementation of Starlark permits `while` loops only if the `-recursion` flag is enabled.
2846A `while` statement is permitted at top level if the `-globalreassign` flag is enabled.
2847
2848
2849### For loops
2850
2851A `for` loop evaluates its operand, which must be an iterable value.
2852Then, for each element of the iterable's sequence, the loop assigns
2853the successive element values to one or more variables and executes a
2854list of statements, the _loop body_.
2855
2856```grammar {.good}
2857ForStmt = 'for' LoopVariables 'in' Expression ':' Suite .
2858```
2859
2860Example:
2861
2862```python
2863for x in range(10):
2864   print(10)
2865```
2866
2867The assignment of each value to the loop variables follows the same
2868rules as an ordinary assignment.  In this example, two-element lists
2869are repeatedly assigned to the pair of variables (a, i):
2870
2871```python
2872for a, i in [["a", 1], ["b", 2], ["c", 3]]:
2873  print(a, i)                          # prints "a 1", "b 2", "c 3"
2874```
2875
2876Because Starlark loops always iterate over a finite sequence, they are
2877guaranteed to terminate, unlike loops in most languages which can
2878execute an arbitrary and perhaps unbounded number of iterations.
2879
2880Within the body of a `for` loop, `break` and `continue` statements may
2881be used to stop the execution of the loop or advance to the next
2882iteration.
2883
2884In Starlark, a `for` loop is permitted only within a function definition.
2885A `for` loop at top level results in a static error.
2886
2887<b>Implementation note:</b>
2888The Go implementation of Starlark permits loops to appear at top level
2889if the `-globalreassign` flag is enabled.
2890
2891
2892### Break and Continue
2893
2894The `break` and `continue` statements terminate the current iteration
2895of a `for` loop.  Whereas the `continue` statement resumes the loop at
2896the next iteration, a `break` statement terminates the entire loop.
2897
2898```grammar {.good}
2899BreakStmt    = 'break' .
2900ContinueStmt = 'continue' .
2901```
2902
2903Example:
2904
2905```python
2906for x in range(10):
2907    if x%2 == 1:
2908        continue        # skip odd numbers
2909    if x > 7:
2910        break           # stop at 8
2911    print(x)            # prints "0", "2", "4", "6"
2912```
2913
2914Both statements affect only the innermost lexically enclosing loop.
2915It is a static error to use a `break` or `continue` statement outside a
2916loop.
2917
2918
2919### Load statements
2920
2921The `load` statement loads another Starlark module, extracts one or
2922more values from it, and binds them to names in the current module.
2923
2924<!--
2925The awkwardness of load statements is a consequence of staying a
2926strict subset of Python syntax, which allows reuse of existing tools
2927such as editor support. Python import statements are inadequate for
2928Starlark because they don't allow arbitrary file names for module names.
2929-->
2930
2931Syntactically, a load statement looks like a function call `load(...)`.
2932
2933```grammar {.good}
2934LoadStmt = 'load' '(' string {',' [identifier '='] string} [','] ')' .
2935```
2936
2937A load statement requires at least two "arguments".
2938The first must be a literal string; it identifies the module to load.
2939Its interpretation is determined by the application into which the
2940Starlark interpreter is embedded, and is not specified here.
2941
2942During execution, the application determines what action to take for a
2943load statement.
2944A typical implementation locates and executes a Starlark file,
2945populating a cache of files executed so far to avoid duplicate work,
2946to obtain a module, which is a mapping from global names to values.
2947
2948The remaining arguments are a mixture of literal strings, such as
2949`"x"`, or named literal strings, such as `y="x"`.
2950
2951The literal string (`"x"`), which must denote a valid identifier not
2952starting with `_`, specifies the name to extract from the loaded
2953module.  In effect, names starting with `_` are not exported.
2954The name (`y`) specifies the local name;
2955if no name is given, the local name matches the quoted name.
2956
2957```python
2958load("module.star", "x", "y", "z")       # assigns x, y, and z
2959load("module.star", "x", y2="y", "z")    # assigns x, y2, and z
2960```
2961
2962A load statement may not be nested inside any other statement.
2963
2964
2965## Module execution
2966
2967Each Starlark file defines a _module_, which is a mapping from the
2968names of global variables to their values.
2969When a Starlark file is executed, whether directly by the application
2970or indirectly through a `load` statement, a new Starlark thread is
2971created, and this thread executes all the top-level statements in the
2972file.
2973Because if-statements and for-loops cannot appear outside of a function,
2974control flows from top to bottom.
2975
2976If execution reaches the end of the file, module initialization is
2977successful.
2978At that point, the value of each of the module's global variables is
2979frozen, rendering subsequent mutation impossible.
2980The module is then ready for use by another Starlark thread, such as
2981one executing a load statement.
2982Such threads may access values or call functions defined in the loaded
2983module.
2984
2985A Starlark thread may carry state on behalf of the application into
2986which it is embedded, and application-defined functions may behave
2987differently depending on this thread state.
2988Because module initialization always occurs in a new thread, thread
2989state is never carried from a higher-level module into a lower-level
2990one.
2991The initialization behavior of a module is thus independent of
2992whichever module triggered its initialization.
2993
2994If a Starlark thread encounters an error, execution stops and the error
2995is reported to the application, along with a backtrace showing the
2996stack of active function calls at the time of the error.
2997If an error occurs during initialization of a Starlark module, any
2998active `load` statements waiting for initialization of the module also
2999fail.
3000
3001Starlark provides no mechanism by which errors can be handled within
3002the language.
3003
3004
3005## Built-in constants and functions
3006
3007The outermost block of the Starlark environment is known as the "predeclared" block.
3008It defines a number of fundamental values and functions needed by all Starlark programs,
3009such as `None`, `True`, `False`, and `len`, and possibly additional
3010application-specific names.
3011
3012These names are not reserved words so Starlark programs are free to
3013redefine them in a smaller block such as a function body or even at
3014the top level of a module.  However, doing so may be confusing to the
3015reader.  Nonetheless, this rule permits names to be added to the
3016predeclared block in later versions of the language (or
3017application-specific dialect) without breaking existing programs.
3018
3019
3020### None
3021
3022`None` is the distinguished value of the type `NoneType`.
3023
3024### True and False
3025
3026`True` and `False` are the two values of type `bool`.
3027
3028### any
3029
3030`any(x)` returns `True` if any element of the iterable sequence x has a truth value of true.
3031If the iterable is empty, it returns `False`.
3032
3033### all
3034
3035`all(x)` returns `False` if any element of the iterable sequence x has a truth value of false.
3036If the iterable is empty, it returns `True`.
3037
3038### bool
3039
3040`bool(x)` interprets `x` as a Boolean value---`True` or `False`.
3041With no argument, `bool()` returns `False`.
3042
3043
3044### chr
3045
3046`chr(i)` returns a string that encodes the single Unicode code point
3047whose value is specified by the integer `i`. `chr` fails unless 0 ≤
3048`i` ≤ 0x10FFFF.
3049
3050Example:
3051
3052```python
3053chr(65)                         # "A",
3054chr(1049)                       # "Й", CYRILLIC CAPITAL LETTER SHORT I
3055chr(0x1F63F)                    # "��", CRYING CAT FACE
3056```
3057
3058See also: `ord`.
3059
3060<b>Implementation note:</b> `chr` is not provided by the Java implementation.
3061
3062### dict
3063
3064`dict` creates a dictionary.  It accepts up to one positional
3065argument, which is interpreted as an iterable of two-element
3066sequences (pairs), each specifying a key/value pair in
3067the resulting dictionary.
3068
3069`dict` also accepts any number of keyword arguments, each of which
3070specifies a key/value pair in the resulting dictionary;
3071each keyword is treated as a string.
3072
3073```python
3074dict()                          # {}, empty dictionary
3075dict([(1, 2), (3, 4)])          # {1: 2, 3: 4}
3076dict([(1, 2), ["a", "b"]])      # {1: 2, "a": "b"}
3077dict(one=1, two=2)              # {"one": 1, "two", 1}
3078dict([(1, 2)], x=3)             # {1: 2, "x": 3}
3079```
3080
3081With no arguments, `dict()` returns a new empty dictionary.
3082
3083`dict(x)` where x is a dictionary returns a new copy of x.
3084
3085### dir
3086
3087`dir(x)` returns a new sorted list of the names of the attributes (fields and methods) of its operand.
3088The attributes of a value `x` are the names `f` such that `x.f` is a valid expression.
3089
3090For example,
3091
3092```python
3093dir("hello")                    # ['capitalize', 'count', ...], the methods of a string
3094```
3095
3096Several types known to the interpreter, such as list, string, and dict, have methods, but none have fields.
3097However, an application may define types with fields that may be read or set by statements such as these:
3098
3099```text
3100y = x.f
3101x.f = y
3102```
3103
3104### enumerate
3105
3106`enumerate(x)` returns a list of (index, value) pairs, each containing
3107successive values of the iterable sequence xand the index of the value
3108within the sequence.
3109
3110The optional second parameter, `start`, specifies an integer value to
3111add to each index.
3112
3113```python
3114enumerate(["zero", "one", "two"])               # [(0, "zero"), (1, "one"), (2, "two")]
3115enumerate(["one", "two"], 1)                    # [(1, "one"), (2, "two")]
3116```
3117
3118### fail
3119
3120The `fail(*args, sep=" ")` function causes execution to fail
3121with the specified error message.
3122Like `print`, arguments are formatted as if by `str(x)` and
3123separated by a space, unless an alternative separator is
3124specified by a `sep` named argument.
3125
3126```python
3127fail("oops")				# "fail: oops"
3128fail("oops", 1, False, sep='/')		# "fail: oops/1/False"
3129```
3130
3131### float
3132
3133`float(x)` interprets its argument as a floating-point number.
3134
3135If x is a `float`, the result is x.
3136if x is an `int`, the result is the nearest floating point value to x.
3137If x is a string, the string is interpreted as a floating-point literal.
3138With no arguments, `float()` returns `0.0`.
3139
3140
3141### getattr
3142
3143`getattr(x, name)` returns the value of the attribute (field or method) of x named `name`.
3144It is a dynamic error if x has no such attribute.
3145
3146`getattr(x, "f")` is equivalent to `x.f`.
3147
3148```python
3149getattr("banana", "split")("a")	       # ["b", "n", "n", ""], equivalent to "banana".split("a")
3150```
3151
3152The three-argument form `getattr(x, name, default)` returns the
3153provided `default` value instead of failing.
3154
3155### hasattr
3156
3157`hasattr(x, name)` reports whether x has an attribute (field or method) named `name`.
3158
3159### hash
3160
3161`hash(x)` returns an integer hash of a string x
3162such that two equal strings have the same hash.
3163In other words `x == y` implies `hash(x) == hash(y)`.
3164
3165In the interests of reproducibility of Starlark program behavior over time and
3166across implementations, the specific hash function is the same as that implemented by
3167[java.lang.String.hashCode](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#hashCode),
3168a simple polynomial accumulator over the UTF-16 transcoding of the string:
3169 ```
3170s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
3171```
3172
3173`hash` fails if given a non-string operand,
3174even if the value is hashable and thus suitable as the key of dictionary.
3175
3176### int
3177
3178`int(x[, base])` interprets its argument as an integer.
3179
3180If x is an `int`, the result is x.
3181If x is a `float`, the result is the integer value nearest to x,
3182truncating towards zero; it is an error if x is not finite (`NaN`,
3183`+Inf`, `-Inf`).
3184If x is a `bool`, the result is 0 for `False` or 1 for `True`.
3185
3186If x is a string, it is interpreted as a sequence of digits in the
3187specified base, decimal by default.
3188If `base` is zero, x is interpreted like an integer literal, the base
3189being inferred from an optional base prefix such as `0b`, `0o`, or
3190`0x` preceding the first digit.
3191When the `base` is provided explictly, a matching base prefix is
3192also permitted, and has no effect.
3193Irrespective of base, the string may start with an optional `+` or `-`
3194sign indicating the sign of the result.
3195
3196```python
3197int("11")               # 11
3198int("11", 0)            # 11
3199int("11", 10)           # 11
3200int("11", 2)            # 3
3201int("11", 8)            # 9
3202int("11", 16)           # 17
3203
3204int("0x11", 0)          # 17
3205int("0x11", 16)         # 17
3206int("0b1", 16)          # 177 (0xb1)
3207int("0b1", 2)           # 1
3208int("0b1", 0)           # 1
3209
3210int("0x11")             # error: invalid literal with base 10
3211```
3212
3213### len
3214
3215`len(x)` returns the number of elements in its argument.
3216
3217It is a dynamic error if its argument is not a sequence.
3218
3219### list
3220
3221`list` constructs a list.
3222
3223`list(x)` returns a new list containing the elements of the
3224iterable sequence x.
3225
3226With no argument, `list()` returns a new empty list.
3227
3228### max
3229
3230`max(x)` returns the greatest element in the iterable sequence x.
3231
3232It is an error if any element does not support ordered comparison,
3233or if the sequence is empty.
3234
3235The optional named parameter `key` specifies a function to be applied
3236to each element prior to comparison.
3237
3238```python
3239max([3, 1, 4, 1, 5, 9])                         # 9
3240max("two", "three", "four")                     # "two", the lexicographically greatest
3241max("two", "three", "four", key=len)            # "three", the longest
3242```
3243
3244### min
3245
3246`min(x)` returns the least element in the iterable sequence x.
3247
3248It is an error if any element does not support ordered comparison,
3249or if the sequence is empty.
3250
3251```python
3252min([3, 1, 4, 1, 5, 9])                         # 1
3253min("two", "three", "four")                     # "four", the lexicographically least
3254min("two", "three", "four", key=len)            # "two", the shortest
3255```
3256
3257
3258### ord
3259
3260`ord(s)` returns the integer value of the sole Unicode code point encoded by the string `s`.
3261
3262If `s` does not encode exactly one Unicode code point, `ord` fails.
3263Each invalid code within the string is treated as if it encodes the
3264Unicode replacement character, U+FFFD.
3265
3266Example:
3267
3268```python
3269ord("A")				# 65
3270ord("Й")				# 1049
3271ord("��")					# 0x1F63F
3272ord("Й"[1:])				# 0xFFFD (Unicode replacement character)
3273```
3274
3275See also: `chr`.
3276
3277<b>Implementation note:</b> `ord` is not provided by the Java implementation.
3278
3279### print
3280
3281`print(*args, sep=" ")` prints its arguments, followed by a newline.
3282Arguments are formatted as if by `str(x)` and separated with a space,
3283unless an alternative separator is specified by a `sep` named argument.
3284
3285Example:
3286
3287```python
3288print(1, "hi")		       		# "1 hi\n"
3289print("hello", "world")			# "hello world\n"
3290print("hello", "world", sep=", ")	# "hello, world\n"
3291```
3292
3293Typically the formatted string is printed to the standard error file,
3294but the exact behavior is a property of the Starlark thread and is
3295determined by the host application.
3296
3297### range
3298
3299`range` returns an immutable sequence of integers defined by the specified interval and stride.
3300
3301```python
3302range(stop)                             # equivalent to range(0, stop)
3303range(start, stop)                      # equivalent to range(start, stop, 1)
3304range(start, stop, step)
3305```
3306
3307`range` requires between one and three integer arguments.
3308With one argument, `range(stop)` returns the ascending sequence of non-negative integers less than `stop`.
3309With two arguments, `range(start, stop)` returns only integers not less than `start`.
3310
3311With three arguments, `range(start, stop, step)` returns integers
3312formed by successively adding `step` to `start` until the value meets or passes `stop`.
3313A call to `range` fails if the value of `step` is zero.
3314
3315A call to `range` does not materialize the entire sequence, but
3316returns a fixed-size value of type `"range"` that represents the
3317parameters that define the sequence.
3318The `range` value is iterable and may be indexed efficiently.
3319
3320```python
3321list(range(10))                         # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3322list(range(3, 10))                      # [3, 4, 5, 6, 7, 8, 9]
3323list(range(3, 10, 2))                   # [3, 5, 7, 9]
3324list(range(10, 3, -2))                  # [10, 8, 6, 4]
3325```
3326
3327The `len` function applied to a `range` value returns its length.
3328The truth value of a `range` value is `True` if its length is non-zero.
3329
3330Range values are comparable: two `range` values compare equal if they
3331denote the same sequence of integers, even if they were created using
3332different parameters.
3333
3334Range values are not hashable.  <!-- should they be? -->
3335
3336The `str` function applied to a `range` value yields a string of the
3337form `range(10)`, `range(1, 10)`, or `range(1, 10, 2)`.
3338
3339The `x in y` operator, where `y` is a range, reports whether `x` is equal to
3340some member of the sequence `y`; the operation fails unless `x` is a
3341number.
3342
3343### repr
3344
3345`repr(x)` formats its argument as a string.
3346
3347All strings in the result are double-quoted.
3348
3349```python
3350repr(1)                 # '1'
3351repr("x")               # '"x"'
3352repr([1, "x"])          # '[1, "x"]'
3353```
3354
3355### reversed
3356
3357`reversed(x)` returns a new list containing the elements of the iterable sequence x in reverse order.
3358
3359```python
3360reversed(range(5))                              # [4, 3, 2, 1, 0]
3361reversed("stressed".codepoints())               # ["d", "e", "s", "s", "e", "r", "t", "s"]
3362reversed({"one": 1, "two": 2}.keys())           # ["two", "one"]
3363```
3364
3365### set
3366
3367`set(x)` returns a new set containing the elements of the iterable x.
3368With no argument, `set()` returns a new empty set.
3369
3370```python
3371set([3, 1, 4, 1, 5, 9])         # set([3, 1, 4, 5, 9])
3372```
3373
3374<b>Implementation note:</b>
3375Sets are an optional feature of the Go implementation of Starlark,
3376enabled by the `-set` flag.
3377
3378
3379### sorted
3380
3381`sorted(x)` returns a new list containing the elements of the iterable sequence x,
3382in sorted order.  The sort algorithm is stable.
3383
3384The optional named parameter `reverse`, if true, causes `sorted` to
3385return results in reverse sorted order.
3386
3387The optional named parameter `key` specifies a function of one
3388argument to apply to obtain the value's sort key.
3389The default behavior is the identity function.
3390
3391```python
3392sorted(set("harbors".codepoints()))                             # ['a', 'b', 'h', 'o', 'r', 's']
3393sorted([3, 1, 4, 1, 5, 9])                                      # [1, 1, 3, 4, 5, 9]
3394sorted([3, 1, 4, 1, 5, 9], reverse=True)                        # [9, 5, 4, 3, 1, 1]
3395
3396sorted(["two", "three", "four"], key=len)                       # ["two", "four", "three"], shortest to longest
3397sorted(["two", "three", "four"], key=len, reverse=True)         # ["three", "four", "two"], longest to shortest
3398```
3399
3400
3401### str
3402
3403`str(x)` formats its argument as a string.
3404
3405If x is a string, the result is x (without quotation).
3406All other strings, such as elements of a list of strings, are double-quoted.
3407
3408```python
3409str(1)                          # '1'
3410str("x")                        # 'x'
3411str([1, "x"])                   # '[1, "x"]'
3412```
3413
3414### tuple
3415
3416`tuple(x)` returns a tuple containing the elements of the iterable x.
3417
3418With no arguments, `tuple()` returns the empty tuple.
3419
3420### type
3421
3422type(x) returns a string describing the type of its operand.
3423
3424```python
3425type(None)              # "NoneType"
3426type(0)                 # "int"
3427type(0.0)               # "float"
3428```
3429
3430### zip
3431
3432`zip()` returns a new list of n-tuples formed from corresponding
3433elements of each of the n iterable sequences provided as arguments to
3434`zip`.  That is, the first tuple contains the first element of each of
3435the sequences, the second element contains the second element of each
3436of the sequences, and so on.  The result list is only as long as the
3437shortest of the input sequences.
3438
3439```python
3440zip()                                   # []
3441zip(range(5))                           # [(0,), (1,), (2,), (3,), (4,)]
3442zip(range(5), "abc")                    # [(0, "a"), (1, "b"), (2, "c")]
3443```
3444
3445## Built-in methods
3446
3447This section lists the methods of built-in types.  Methods are selected
3448using [dot expressions](#dot-expressions).
3449For example, strings have a `count` method that counts
3450occurrences of a substring; `"banana".count("a")` yields `3`.
3451
3452As with built-in functions, built-in methods accept only positional
3453arguments except where noted.
3454The parameter names serve merely as documentation.
3455
3456
3457<a id='dict·clear'></a>
3458### dict·clear
3459
3460`D.clear()` removes all the entries of dictionary D and returns `None`.
3461It fails if the dictionary is frozen or if there are active iterators.
3462
3463```python
3464x = {"one": 1, "two": 2}
3465x.clear()                               # None
3466print(x)                                # {}
3467```
3468
3469<a id='dict·get'></a>
3470### dict·get
3471
3472`D.get(key[, default])` returns the dictionary value corresponding to the given key.
3473If the dictionary contains no such value, `get` returns `None`, or the
3474value of the optional `default` parameter if present.
3475
3476`get` fails if `key` is unhashable, or the dictionary is frozen or has active iterators.
3477
3478```python
3479x = {"one": 1, "two": 2}
3480x.get("one")                            # 1
3481x.get("three")                          # None
3482x.get("three", 0)                       # 0
3483```
3484
3485<a id='dict·items'></a>
3486### dict·items
3487
3488`D.items()` returns a new list of key/value pairs, one per element in
3489dictionary D, in the same order as they would be returned by a `for` loop.
3490
3491```python
3492x = {"one": 1, "two": 2}
3493x.items()                               # [("one", 1), ("two", 2)]
3494```
3495
3496<a id='dict·keys'></a>
3497### dict·keys
3498
3499`D.keys()` returns a new list containing the keys of dictionary D, in the
3500same order as they would be returned by a `for` loop.
3501
3502```python
3503x = {"one": 1, "two": 2}
3504x.keys()                               # ["one", "two"]
3505```
3506
3507<a id='dict·pop'></a>
3508### dict·pop
3509
3510`D.pop(key[, default])` returns the value corresponding to the specified
3511key, and removes it from the dictionary.  If the dictionary contains no
3512such value, and the optional `default` parameter is present, `pop`
3513returns that value; otherwise, it fails.
3514
3515`pop` fails if `key` is unhashable, or the dictionary is frozen or has active iterators.
3516
3517```python
3518x = {"one": 1, "two": 2}
3519x.pop("one")                            # 1
3520x                                       # {"two": 2}
3521x.pop("three", 0)                       # 0
3522x.pop("four")                           # error: missing key
3523```
3524
3525<a id='dict·popitem'></a>
3526### dict·popitem
3527
3528`D.popitem()` returns the first key/value pair, removing it from the dictionary.
3529
3530`popitem` fails if the dictionary is empty, frozen, or has active iterators.
3531
3532```python
3533x = {"one": 1, "two": 2}
3534x.popitem()                             # ("one", 1)
3535x.popitem()                             # ("two", 2)
3536x.popitem()                             # error: empty dict
3537```
3538
3539<a id='dict·setdefault'></a>
3540### dict·setdefault
3541
3542`D.setdefault(key[, default])` returns the dictionary value corresponding to the given key.
3543If the dictionary contains no such value, `setdefault`, like `get`,
3544returns `None` or the value of the optional `default` parameter if
3545present; `setdefault` additionally inserts the new key/value entry into the dictionary.
3546
3547`setdefault` fails if the key is unhashable, or if the dictionary is frozen or has active iterators.
3548
3549```python
3550x = {"one": 1, "two": 2}
3551x.setdefault("one")                     # 1
3552x.setdefault("three", 0)                # 0
3553x                                       # {"one": 1, "two": 2, "three": 0}
3554x.setdefault("four")                    # None
3555x                                       # {"one": 1, "two": 2, "three": None}
3556```
3557
3558<a id='dict·update'></a>
3559### dict·update
3560
3561`D.update([pairs][, name=value[, ...])` makes a sequence of key/value
3562insertions into dictionary D, then returns `None.`
3563
3564If the positional argument `pairs` is present, it must be `None`,
3565another `dict`, or some other iterable.
3566If it is another `dict`, then its key/value pairs are inserted into D.
3567If it is an iterable, it must provide a sequence of pairs (or other iterables of length 2),
3568each of which is treated as a key/value pair to be inserted into D.
3569
3570For each `name=value` argument present, the name is converted to a
3571string and used as the key for an insertion into D, with its corresponding
3572value being `value`.
3573
3574`update` fails if the dictionary is frozen or has active iterators.
3575
3576```python
3577x = {}
3578x.update([("a", 1), ("b", 2)], c=3)
3579x.update({"d": 4})
3580x.update(e=5)
3581x                                       # {"a": 1, "b": "2", "c": 3, "d": 4, "e": 5}
3582```
3583
3584<a id='dict·values'></a>
3585### dict·values
3586
3587`D.values()` returns a new list containing the dictionary's values, in the
3588same order as they would be returned by a `for` loop over the
3589dictionary.
3590
3591```python
3592x = {"one": 1, "two": 2}
3593x.values()                              # [1, 2]
3594```
3595
3596<a id='list·append'></a>
3597### list·append
3598
3599`L.append(x)` appends `x` to the list L, and returns `None`.
3600
3601`append` fails if the list is frozen or has active iterators.
3602
3603```python
3604x = []
3605x.append(1)                             # None
3606x.append(2)                             # None
3607x.append(3)                             # None
3608x                                       # [1, 2, 3]
3609```
3610
3611<a id='list·clear'></a>
3612### list·clear
3613
3614`L.clear()` removes all the elements of the list L and returns `None`.
3615It fails if the list is frozen or if there are active iterators.
3616
3617```python
3618x = [1, 2, 3]
3619x.clear()                               # None
3620x                                       # []
3621```
3622
3623<a id='list·extend'></a>
3624### list·extend
3625
3626`L.extend(x)` appends the elements of `x`, which must be iterable, to
3627the list L, and returns `None`.
3628
3629`extend` fails if `x` is not iterable, or if the list L is frozen or has active iterators.
3630
3631```python
3632x = []
3633x.extend([1, 2, 3])                     # None
3634x.extend(["foo"])                       # None
3635x                                       # [1, 2, 3, "foo"]
3636```
3637
3638<a id='list·index'></a>
3639### list·index
3640
3641`L.index(x[, start[, end]])` finds `x` within the list L and returns its index.
3642
3643The optional `start` and `end` parameters restrict the portion of
3644list L that is inspected.  If provided and not `None`, they must be list
3645indices of type `int`. If an index is negative, `len(L)` is effectively
3646added to it, then if the index is outside the range `[0:len(L)]`, the
3647nearest value within that range is used; see [Indexing](#indexing).
3648
3649`index` fails if `x` is not found in L, or if `start` or `end`
3650is not a valid index (`int` or `None`).
3651
3652```python
3653x = list("banana".codepoints())
3654x.index("a")                            # 1 (bAnana)
3655x.index("a", 2)                         # 3 (banAna)
3656x.index("a", -2)                        # 5 (bananA)
3657```
3658
3659<a id='list·insert'></a>
3660### list·insert
3661
3662`L.insert(i, x)` inserts the value `x` in the list L at index `i`, moving
3663higher-numbered elements along by one.  It returns `None`.
3664
3665As usual, the index `i` must be an `int`. If its value is negative,
3666the length of the list is added, then its value is clamped to the
3667nearest value in the range `[0:len(L)]` to yield the effective index.
3668
3669`insert` fails if the list is frozen or has active iterators.
3670
3671```python
3672x = ["b", "c", "e"]
3673x.insert(0, "a")                        # None
3674x.insert(-1, "d")                       # None
3675x                                       # ["a", "b", "c", "d", "e"]
3676```
3677
3678<a id='list·pop'></a>
3679### list·pop
3680
3681`L.pop([index])` removes and returns the last element of the list L, or,
3682if the optional index is provided, at that index.
3683
3684`pop` fails if the index is not valid for `L[i]`,
3685or if the list is frozen or has active iterators.
3686
3687```python
3688x = [1, 2, 3, 4, 5]
3689x.pop()                                 # 5
3690x                                       # [1, 2, 3, 4]
3691x.pop(-2)                               # 3
3692x                                       # [1, 2, 4]
3693x.pop(-3)                               # 1
3694x                                       # [2, 4]
3695x.pop()                                 # 4
3696x                                       # [2]
3697```
3698
3699<a id='list·remove'></a>
3700### list·remove
3701
3702`L.remove(x)` removes the first occurrence of the value `x` from the list L, and returns `None`.
3703
3704`remove` fails if the list does not contain `x`, is frozen, or has active iterators.
3705
3706```python
3707x = [1, 2, 3, 2]
3708x.remove(2)                             # None (x == [1, 3, 2])
3709x.remove(2)                             # None (x == [1, 3])
3710x.remove(2)                             # error: element not found
3711```
3712
3713<a id='set·union'></a>
3714### set·union
3715
3716`S.union(iterable)` returns a new set into which have been inserted
3717all the elements of set S and all the elements of the argument, which
3718must be iterable.
3719
3720`union` fails if any element of the iterable is not hashable.
3721
3722```python
3723x = set([1, 2])
3724y = set([2, 3])
3725x.union(y)                              # set([1, 2, 3])
3726```
3727
3728<a id='string·elem_ords'></a>
3729### string·elem_ords
3730
3731`S.elem_ords()` returns an iterable value containing the
3732sequence of numeric bytes values in the string S.
3733
3734To materialize the entire sequence of bytes, apply `list(...)` to the result.
3735
3736Example:
3737
3738```python
3739list("Hello, 世界".elem_ords())        # [72, 101, 108, 108, 111, 44, 32, 228, 184, 150, 231, 149, 140]
3740```
3741
3742See also: `string·elems`.
3743
3744<b>Implementation note:</b> `elem_ords` is not provided by the Java implementation.
3745
3746<a id='string·capitalize'></a>
3747### string·capitalize
3748
3749`S.capitalize()` returns a copy of string S with its first code point
3750changed to its title case and all subsequent letters changed to their
3751lower case.
3752
3753```python
3754"hello, world!".capitalize()		# "Hello, world!"
3755"hElLo, wOrLd!".capitalize()		# "Hello, world!"
3756"¿Por qué?".capitalize()		# "¿por qué?"
3757```
3758
3759<a id='string·codepoint_ords'></a>
3760### string·codepoint_ords
3761
3762`S.codepoint_ords()` returns an iterable value containing the
3763sequence of integer Unicode code points encoded by the string S.
3764Each invalid code within the string is treated as if it encodes the
3765Unicode replacement character, U+FFFD.
3766
3767By returning an iterable, not a list, the cost of decoding the string
3768is deferred until actually needed; apply `list(...)` to the result to
3769materialize the entire sequence.
3770
3771Example:
3772
3773```python
3774list("Hello, 世界".codepoint_ords())        # [72, 101, 108, 108, 111, 44, 32, 19990, 30028]
3775
3776for cp in "Hello, 世界".codepoint_ords():
3777   print(chr(cp))  # prints 'H', 'e', 'l', 'l', 'o', ',', ' ', '世', '界'
3778```
3779
3780See also: `string·codepoints`.
3781
3782<b>Implementation note:</b> `codepoint_ords` is not provided by the Java implementation.
3783
3784<a id='string·count'></a>
3785### string·count
3786
3787`S.count(sub[, start[, end]])` returns the number of occcurences of
3788`sub` within the string S, or, if the optional substring indices
3789`start` and `end` are provided, within the designated substring of S.
3790They are interpreted according to Starlark's [indexing conventions](#indexing).
3791
3792```python
3793"hello, world!".count("o")              # 2
3794"hello, world!".count("o", 7, 12)       # 1  (in "world")
3795```
3796
3797<a id='string·endswith'></a>
3798### string·endswith
3799
3800`S.endswith(suffix[, start[, end]])` reports whether the string
3801`S[start:end]` has the specified suffix.
3802
3803```python
3804"filename.star".endswith(".star")         # True
3805```
3806
3807The `suffix` argument may be a tuple of strings, in which case the
3808function reports whether any one of them is a suffix.
3809
3810```python
3811'foo.cc'.endswith(('.cc', '.h'))         # True
3812```
3813
3814
3815<a id='string·find'></a>
3816### string·find
3817
3818`S.find(sub[, start[, end]])` returns the index of the first
3819occurrence of the substring `sub` within S.
3820
3821If either or both of `start` or `end` are specified,
3822they specify a subrange of S to which the search should be restricted.
3823They are interpreted according to Starlark's [indexing conventions](#indexing).
3824
3825If no occurrence is found, `found` returns -1.
3826
3827```python
3828"bonbon".find("on")             # 1
3829"bonbon".find("on", 2)          # 4
3830"bonbon".find("on", 2, 5)       # -1
3831```
3832
3833<a id='string·format'></a>
3834### string·format
3835
3836`S.format(*args, **kwargs)` returns a version of the format string S
3837in which bracketed portions `{...}` are replaced
3838by arguments from `args` and `kwargs`.
3839
3840Within the format string, a pair of braces `{{` or `}}` is treated as
3841a literal open or close brace.
3842Each unpaired open brace must be matched by a close brace `}`.
3843The optional text between corresponding open and close braces
3844specifies which argument to use and how to format it, and consists of
3845three components, all optional:
3846a field name, a conversion preceded by '`!`', and a format specifier
3847preceded by '`:`'.
3848
3849```text
3850{field}
3851{field:spec}
3852{field!conv}
3853{field!conv:spec}
3854```
3855
3856The *field name* may be either a decimal number or a keyword.
3857A number is interpreted as the index of a positional argument;
3858a keyword specifies the value of a keyword argument.
3859If all the numeric field names form the sequence 0, 1, 2, and so on,
3860they may be omitted and those values will be implied; however,
3861the explicit and implicit forms may not be mixed.
3862
3863The *conversion* specifies how to convert an argument value `x` to a
3864string. It may be either `!r`, which converts the value using
3865`repr(x)`, or `!s`, which converts the value using `str(x)` and is
3866the default.
3867
3868The *format specifier*, after a colon, specifies field width,
3869alignment, padding, and numeric precision.
3870Currently it must be empty, but it is reserved for future use.
3871
3872```python
3873"a{x}b{y}c{}".format(1, x=2, y=3)               # "a2b3c1"
3874"a{}b{}c".format(1, 2)                          # "a1b2c"
3875"({1}, {0})".format("zero", "one")              # "(one, zero)"
3876"Is {0!r} {0!s}?".format('heterological')       # 'is "heterological" heterological?'
3877```
3878
3879<a id='string·index'></a>
3880### string·index
3881
3882`S.index(sub[, start[, end]])` returns the index of the first
3883occurrence of the substring `sub` within S, like `S.find`, except
3884that if the substring is not found, the operation fails.
3885
3886```python
3887"bonbon".index("on")             # 1
3888"bonbon".index("on", 2)          # 4
3889"bonbon".index("on", 2, 5)       # error: substring not found  (in "nbo")
3890```
3891
3892<a id='string·isalnum'></a>
3893### string·isalnum
3894
3895`S.isalnum()` reports whether the string S is non-empty and consists only
3896Unicode letters and digits.
3897
3898```python
3899"base64".isalnum()              # True
3900"Catch-22".isalnum()            # False
3901```
3902
3903<a id='string·isalpha'></a>
3904### string·isalpha
3905
3906`S.isalpha()` reports whether the string S is non-empty and consists only of Unicode letters.
3907
3908```python
3909"ABC".isalpha()                 # True
3910"Catch-22".isalpha()            # False
3911"".isalpha()                    # False
3912```
3913
3914<a id='string·isdigit'></a>
3915### string·isdigit
3916
3917`S.isdigit()` reports whether the string S is non-empty and consists only of Unicode digits.
3918
3919```python
3920"123".isdigit()                 # True
3921"Catch-22".isdigit()            # False
3922"".isdigit()                    # False
3923```
3924
3925<a id='string·islower'></a>
3926### string·islower
3927
3928`S.islower()` reports whether the string S contains at least one cased Unicode
3929letter, and all such letters are lowercase.
3930
3931```python
3932"hello, world".islower()        # True
3933"Catch-22".islower()            # False
3934"123".islower()                 # False
3935```
3936
3937<a id='string·isspace'></a>
3938### string·isspace
3939
3940`S.isspace()` reports whether the string S is non-empty and consists only of Unicode spaces.
3941
3942```python
3943"    ".isspace()                # True
3944"\r\t\n".isspace()              # True
3945"".isspace()                    # False
3946```
3947
3948<a id='string·istitle'></a>
3949### string·istitle
3950
3951`S.istitle()` reports whether the string S contains at least one cased Unicode
3952letter, and all such letters that begin a word are in title case.
3953
3954```python
3955"Hello, World!".istitle()       # True
3956"Catch-22".istitle()            # True
3957"HAL-9000".istitle()            # False
3958"Dženan".istitle()		# True
3959"DŽenan".istitle()		# False ("DŽ" is a single Unicode letter)
3960"123".istitle()                 # False
3961```
3962
3963<a id='string·isupper'></a>
3964### string·isupper
3965
3966`S.isupper()` reports whether the string S contains at least one cased Unicode
3967letter, and all such letters are uppercase.
3968
3969```python
3970"HAL-9000".isupper()            # True
3971"Catch-22".isupper()            # False
3972"123".isupper()                 # False
3973```
3974
3975<a id='string·join'></a>
3976### string·join
3977
3978`S.join(iterable)` returns the string formed by concatenating each
3979element of its argument, with a copy of the string S between
3980successive elements. The argument must be an iterable whose elements
3981are strings.
3982
3983```python
3984", ".join(["one", "two", "three"])      # "one, two, three"
3985"a".join("ctmrn".codepoints())          # "catamaran"
3986```
3987
3988<a id='string·lower'></a>
3989### string·lower
3990
3991`S.lower()` returns a copy of the string S with letters converted to lowercase.
3992
3993```python
3994"Hello, World!".lower()                 # "hello, world!"
3995```
3996
3997<a id='string·lstrip'></a>
3998### string·lstrip
3999
4000`S.lstrip()` returns a copy of the string S with leading whitespace removed.
4001
4002Like `strip`, it accepts an optional string parameter that specifies an
4003alternative set of Unicode code points to remove.
4004
4005```python
4006"  hello  ".lstrip()                    # "hello  "
4007"  hello  ".lstrip("h o")               # "ello  "
4008```
4009
4010<a id='string·partition'></a>
4011### string·partition
4012
4013`S.partition(x)` splits string S into three parts and returns them as
4014a tuple: the portion before the first occurrence of string `x`, `x` itself,
4015and the portion following it.
4016If S does not contain `x`, `partition` returns `(S, "", "")`.
4017
4018`partition` fails if `x` is not a string, or is the empty string.
4019
4020```python
4021"one/two/three".partition("/")		# ("one", "/", "two/three")
4022```
4023
4024<a id='string·replace'></a>
4025### string·replace
4026
4027`S.replace(old, new[, count])` returns a copy of string S with all
4028occurrences of substring `old` replaced by `new`. If the optional
4029argument `count`, which must be an `int`, is non-negative, it
4030specifies a maximum number of occurrences to replace.
4031
4032```python
4033"banana".replace("a", "o")		# "bonono"
4034"banana".replace("a", "o", 2)		# "bonona"
4035```
4036
4037<a id='string·rfind'></a>
4038### string·rfind
4039
4040`S.rfind(sub[, start[, end]])` returns the index of the substring `sub` within
4041S, like `S.find`, except that `rfind` returns the index of the substring's
4042_last_ occurrence.
4043
4044```python
4045"bonbon".rfind("on")             # 4
4046"bonbon".rfind("on", None, 5)    # 1
4047"bonbon".rfind("on", 2, 5)       # -1
4048```
4049
4050<a id='string·rindex'></a>
4051### string·rindex
4052
4053`S.rindex(sub[, start[, end]])` returns the index of the substring `sub` within
4054S, like `S.index`, except that `rindex` returns the index of the substring's
4055_last_ occurrence.
4056
4057```python
4058"bonbon".rindex("on")             # 4
4059"bonbon".rindex("on", None, 5)    # 1                           (in "bonbo")
4060"bonbon".rindex("on", 2, 5)       # error: substring not found  (in "nbo")
4061```
4062
4063<a id='string·rpartition'></a>
4064### string·rpartition
4065
4066`S.rpartition(x)` is like `partition`, but splits `S` at the last occurrence of `x`.
4067
4068```python
4069"one/two/three".partition("/")		# ("one/two", "/", "three")
4070```
4071
4072<a id='string·rsplit'></a>
4073### string·rsplit
4074
4075`S.rsplit([sep[, maxsplit]])` splits a string into substrings like `S.split`,
4076except that when a maximum number of splits is specified, `rsplit` chooses the
4077rightmost splits.
4078
4079```python
4080"banana".rsplit("n")                         # ["ba", "a", "a"]
4081"banana".rsplit("n", 1)                      # ["bana", "a"]
4082"one two  three".rsplit(None, 1)             # ["one two", "three"]
4083"".rsplit("n")                               # [""]
4084```
4085
4086<a id='string·rstrip'></a>
4087### string·rstrip
4088
4089`S.rstrip()` returns a copy of the string S with trailing whitespace removed.
4090
4091Like `strip`, it accepts an optional string parameter that specifies an
4092alternative set of Unicode code points to remove.
4093
4094```python
4095"  hello  ".rstrip()                    # "  hello"
4096"  hello  ".rstrip("h o")               # "  hell"
4097```
4098
4099<a id='string·split'></a>
4100### string·split
4101
4102`S.split([sep [, maxsplit]])` returns the list of substrings of S,
4103splitting at occurrences of the delimiter string `sep`.
4104
4105Consecutive occurrences of `sep` are considered to delimit empty
4106strings, so `'food'.split('o')` returns `['f', '', 'd']`.
4107Splitting an empty string with a specified separator returns `['']`.
4108If `sep` is the empty string, `split` fails.
4109
4110If `sep` is not specified or is `None`, `split` uses a different
4111algorithm: it removes all leading spaces from S
4112(or trailing spaces in the case of `rsplit`),
4113then splits the string around each consecutive non-empty sequence of
4114Unicode white space characters.
4115If S consists only of white space, `S.split()` returns the empty list.
4116
4117If `maxsplit` is given and non-negative, it specifies a maximum number of splits.
4118
4119```python
4120"one two  three".split()                    # ["one", "two", "three"]
4121"one two  three".split(" ")                 # ["one", "two", "", "three"]
4122"one two  three".split(None, 1)             # ["one", "two  three"]
4123"banana".split("n")                         # ["ba", "a", "a"]
4124"banana".split("n", 1)                      # ["ba", "ana"]
4125"".split("n")                               # [""]
4126```
4127
4128<a id='string·elems'></a>
4129### string·elems
4130
4131`S.elems()` returns an iterable value containing successive
41321-byte substrings of S.
4133To materialize the entire sequence, apply `list(...)` to the result.
4134
4135Example:
4136
4137```python
4138list('Hello, 世界'.elems())  # ["H", "e", "l", "l", "o", ",", " ", "\xe4", "\xb8", "\x96", "\xe7", "\x95", "\x8c"]
4139```
4140
4141See also: `string·elem_ords`.
4142
4143
4144<a id='string·codepoints'></a>
4145### string·codepoints
4146
4147`S.codepoints()` returns an iterable value containing the sequence of
4148substrings of S that each encode a single Unicode code point.
4149Each invalid code within the string is treated as if it encodes the
4150Unicode replacement character, U+FFFD.
4151
4152By returning an iterable, not a list, the cost of decoding the string
4153is deferred until actually needed; apply `list(...)` to the result to
4154materialize the entire sequence.
4155
4156Example:
4157
4158```python
4159list('Hello, 世界'.codepoints())  # ['H', 'e', 'l', 'l', 'o', ',', ' ', '世', '界']
4160
4161for cp in 'Hello, 世界'.codepoints():
4162   print(cp)  # prints 'H', 'e', 'l', 'l', 'o', ',', ' ', '世', '界'
4163```
4164
4165See also: `string·codepoint_ords`.
4166
4167<b>Implementation note:</b> `codepoints` is not provided by the Java implementation.
4168
4169<a id='string·splitlines'></a>
4170### string·splitlines
4171
4172`S.splitlines([keepends])` returns a list whose elements are the
4173successive lines of S, that is, the strings formed by splitting S at
4174line terminators (currently assumed to be a single newline, `\n`,
4175regardless of platform).
4176
4177The optional argument, `keepends`, is interpreted as a Boolean.
4178If true, line terminators are preserved in the result, though
4179the final element does not necessarily end with a line terminator.
4180
4181As a special case, if S is the empty string,
4182`splitlines` returns the empty list.
4183
4184```python
4185"one\n\ntwo".splitlines()       # ["one", "", "two"]
4186"one\n\ntwo".splitlines(True)   # ["one\n", "\n", "two"]
4187"".splitlines()                 # [] -- a special case
4188```
4189
4190<a id='string·startswith'></a>
4191### string·startswith
4192
4193`S.startswith(prefix[, start[, end]])` reports whether the string
4194`S[start:end]` has the specified prefix.
4195
4196```python
4197"filename.star".startswith("filename")         # True
4198```
4199
4200The `prefix` argument may be a tuple of strings, in which case the
4201function reports whether any one of them is a prefix.
4202
4203```python
4204'abc'.startswith(('a', 'A'))                  # True
4205'ABC'.startswith(('a', 'A'))                  # True
4206'def'.startswith(('a', 'A'))                  # False
4207```
4208
4209<a id='string·strip'></a>
4210### string·strip
4211
4212`S.strip()` returns a copy of the string S with leading and trailing whitespace removed.
4213
4214It accepts an optional string argument:
4215`S.strip(cutset)` instead removes all leading
4216and trailing Unicode code points contained in `cutset`.
4217
4218```python
4219"  hello  ".strip()                     # "hello"
4220"  hello  ".strip("h o")                # "ell"
4221```
4222
4223<a id='string·title'></a>
4224### string·title
4225
4226`S.title()` returns a copy of the string S with letters converted to title case.
4227
4228Letters are converted to upper case at the start of words, lower case elsewhere.
4229
4230```python
4231"hElLo, WoRlD!".title()                 # "Hello, World!"
4232"dženan".title()                        # "Dženan" ("Dž" is a single Unicode letter)
4233```
4234
4235<a id='string·upper'></a>
4236### string·upper
4237
4238`S.upper()` returns a copy of the string S with letters converted to uppercase.
4239
4240```python
4241"Hello, World!".upper()                 # "HELLO, WORLD!"
4242```
4243
4244## Dialect differences
4245
4246The list below summarizes features of the Go implementation that are
4247known to differ from the Java implementation of Starlark used by Bazel.
4248Some of these features may be controlled by global options to allow
4249applications to mimic the Bazel dialect more closely. Our goal is
4250eventually to eliminate all such differences on a case-by-case basis.
4251See [Starlark spec issue 20](https://github.com/bazelbuild/starlark/issues/20).
4252
4253* String interpolation supports the `[ioxXc]` conversions.
4254* String elements are bytes.
4255* Non-ASCII strings are encoded using UTF-8.
4256* Strings support hex byte escapes.
4257* Strings have the additional methods `elem_ords`, `codepoint_ords`, and `codepoints`.
4258* The `chr` and `ord` built-in functions are supported.
4259* The `set` built-in function is provided (option: `-set`).
4260* `set & set` and `set | set` compute set intersection and union, respectively.
4261* `assert` is a valid identifier.
4262* `if`, `for`, and `while` are permitted at top level (option: `-globalreassign`).
4263* top-level rebindings are permitted (option: `-globalreassign`).
4264