Python Standard Library — 7. Built in function (v-w-x-y-z)


Previous article: Python Standard Library — 6, built-in function (p-k-r-s-t-u)
Next article: Python Standard Library — 8. Built in constants

Built in functions:

Python Standard Library -- 7. Built in function (v-w-x-y-z)

67. vars([object])

Returns a module, class, instance, or any otherdictObject of propertydictAttribute.

Objects such as modules and instances have updatabledictProperty; however, thedictProperty may be set to restrict writes (for example, the class uses types.mappingproxytype to prevent direct dictionary updates).

Without parameters, vars () behaves like locals (). Note that the locales dictionary works only for reads, because updates to the locales dictionary are ignored.

68. zip(*iterables)

Create an iterator that aggregates elements from each iteratable object.

Returns the iterator of a tuple with the ith tuple containing the ith element from each parameter sequence or iteratable object. When the shortest of the input iteratable objects is exhausted, the iterator stops iterating. When there is only one iteratable object parameter, it returns an iterator of a cell group. Without parameters, it will return an empty iterator. Amount to:

def zip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    rr = object()
    iterators = [iter(it) for it in iterables]
    while iterators:
        result = []
        for it in iterators:
            elem = next(it, sentinel)
            if elem is sentinel:
        yield tuple(result)

The iteratable objects are evaluated from left to right by the function. Make it possible to use zip([iter(s)]N) such a conventional form clusters a series of data into groups of length n. This repeats the same iterator n times so that each tuple output has the result of the nth call to the iterator. Its effect is to split the input into data blocks of length n.

When you don’t have to worry about mismatches at the end of long iteratable objects, zip () uses only inputs of unequal length. If those values are important, use itertools. Zip_longest() instead.

The combination of zip() and * operators can be used to disassemble a list:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)

69. __import__(name, globals=None, locals=None, fromlist=(), level=0)

Annotation is different from importlib. Import_module(), which is a high-level function that is not needed in daily python programming.
This function is called by the import statement. It can be replaced (by importing the builtins module and assigning it to builtins. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\. Also, it is not recommended to use importlib. Import module() instead.

This function imports the name module, possibly using the given globals and locales to determine how to interpret the name in the context of the package. Fromlist gives the name of the object or submodule that should be imported from the module specified by name. The standard implementation does not use its locales parameter at all, but only the globals parameter to determine the package context of the import statement.

Level specifies whether to use absolute or relative import. 0 (the default) means that only absolute imports are performed. A positive value for level indicates the number of levels of the parent directory to search relative to the directory that the module calls \.

When the name variable is in the form of package.module, the package at the highest level (the name before the first dot) is usually returned instead of the module named by name. However, when a non empty fromlist parameter is given, the module named name is returned.

For example, the result of the statement import spam would be bytecode that works the same as:

spam = __import__('spam', globals(), locals(), [], 0)

The result of the statement import spam.ham will be the following call:

spam = __import__('spam.ham', globals(), locals(), [], 0)

Notice how import () returns the top-level module here, because it is bound to an object of a specific name through an import statement.

On the other hand, the statement from spam.ham import eggs, the result of usage as saus will be

_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], 0)
eggs = _temp.eggs
saus = _temp.sausage

Here, the spam.ham module is returned by the \. The objects to be imported are extracted from this object and assigned their corresponding names.

If you only want to import modules by name (possibly in a package), use importlib. Import_module()

Change in version 3.3: negative values for level are no longer supported (which also changes the default value to 0)

The parser only accepts UNIX style line terminators. If you are reading code from a file, make sure to convert the windows or Mac style line breaks with the line break conversion mode.

Previous article: Python Standard Library — 6, built-in function (p-k-r-s-t-u)
Next article: Python Standard Library — 8. Built in constants