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