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