Skip to content

PyBackport: Builtins

py_back allows using the builtins module just as the original. However, they must be imported and initialized by converting the instance:

# Python version lower than 3.9
>>> from py_back.builtins import str

>>> my_string = str("Hello world!")
>>> print(my_string.removesuffix("!"))
Hello world

str

str.removeprefix(prefix, /)

If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string:

from py_back.builtins import str

>>> str('TestHook').removeprefix('Test')
'Hook'

>>> str('BaseTestCase').removeprefix('Test')
'BaseTestCase'

Backported from python 3.9.

str.removesuffix(suffix, /)

If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string:

from py_back.builtins import str

>>> str('MiscTests').removesuffix('Tests')
'Misc'

>>> str('TmpDirMixin').removesuffix('Tests')
'TmpDirMixin'

dict

d | other

Create a new dictionary with the merged keys and values of d and other, which must both be dictionaries. The values of other take priority when d and other share keys.

Backported from python 3.9

d |= other

Update the dictionary d with keys and values from other, which may be either a mapping or an iterable of key/value pairs. The values of other take priority when d and other share keys.

Backported from python 3.9