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