builtins

Starlark module for working with builtins library.

Similar to builtins in Python. Some of functions are from builtins and are not described here.

builtins.bytearray(source, encoding='utf-8', errors='strict')

Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences described in Mutable Sequence Types as well as most methods that the bytes type has, see Bytes and Bytearray Operations.

The optional source parameter can be used to initialize the array in a few different ways:

  • If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

  • If it is an integer, the array will have that size and will be initialized with null bytes.

  • If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array

  • If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

Example:

builtins.bytearray(2) # A byte array of size 2
bytearray(b'\x00\x00')
builtins.bytearray('Hello World','utf-8')
bytearray(b'Hello World')
builtins.bytearray([1, 2, 3, 4, 5])
bytearray(b'     ')

Parameters:

  • source – the source object which has to be converted:

    • if the source is a string, it must be with the encoding parameter

    • if the source is an integer, the array will have that size and will be initialized with null bytes

    • If the source is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

    • if the source is the iterable object, it must have integer elements only in the range 0 to 256.

  • encoding – the encoding of the string if the source is a string.

  • errors – the action to take if the encoding conversion fails.

Returns: an array of bytes.

builtins.bytes(s, encoding='utf-8', errors='strict')

While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256 (attempts to violate this restriction will trigger ValueError). This is done deliberately to emphasise that while many binary formats include ASCII based elements and can be usefully manipulated with some text-oriented algorithms, this is not generally the case for arbitrary binary data (blindly applying text processing algorithms to binary data formats that are not ASCII compatible will usually lead to data corruption).

In addition to the literal forms, bytes objects can be created in a number of other ways:

  • A zero-filled bytes object of a specified length: bytes(10)

  • From an iterable of integers: bytes(range(20))

  • Copying existing binary data via the buffer protocol: bytes(obj)

Since bytes objects are sequences of integers (akin to a tuple), for a bytes object b, b[0] will be an integer, while b[0:1] will be a bytes object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1)

The representation of bytes objects uses the literal format (b’…’) since it is often more useful than e.g. bytes([46, 46, 46]). You can always convert a bytes object into a list of integers using list(b).

Example:

builtins.bytes('Hello World','utf-8')
b'Hello World'
builtins.bytes([1, 2, 3, 4, 5])
b'     '

Parameters:

  • s – the source object which has to be converted.

  • encoding – the encoding required in case object is a string.

  • errors – way to handle error in case the string conversion fails.

Returns:

byte immutable object consisting of unicode 0-256 characters according to src type:

  • integer : Returns array of size initialized to null

  • iterable : Returns array of iterable size with elements equal to iterable elements( 0-256 )

  • string : Returns the encoded string acc. to enc and if encoding fails, performs action according to err specified.

  • no arguments : Returns array of size 0.

builtins.callable(obj)

Return True if the object argument appears callable, False if not. If this returns True, it is still possible that a call fails, but if it is False, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method.

Example:

builtins.callable(str)
True
builtins.callable(10)
False

Parameters:

obj – the object to be checked.

Returns: True if an object is callable else, returns False.

builtins.map(func, iterable)

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

Example:

def square(x):
    return x*x
sqrs_of_numbers=map(square, [1, 2, 3, 4, 5])
next(sqrs_of_numbers)
1
next(sqrs_of_numbers)
4
next(sqrs_of_numbers)
9
next(sqrs_of_numbers)
16
next(sqrs_of_numbers)
25

Parameters:

  • func – the function to be called for each element of the specified iterable.

  • iterable – one or more iterables separated by a comma (such as string, list, tuple, dictionary).

Returns: an iterator object of the map class.

Last updated