github.com/google/skylark@v0.0.0-20181101142754-a5f7082aabed/doc/spec.md (about)

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