Views in Python3.1
In Python3, the role of Views is similar to the role of Views in a database, which can reflect the changes of corresponding objects. After the database table changes, the View query results are the same as the changed values. The same is true for the values queried through Views in python.
In Python3, dict. keys (), dict. items (), dict. values () returns Views instead of list. You can use the list () function to convert Views () to list.
The following is an example of the effect of Views:
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}>>> keys = dishes.keys()>>> values = dishes.values()>>> # iteration>>> n = 0>>> for val in values:... n += val>>> print(n)504>>> # keys and values are iterated over in the same order>>> list(keys)['eggs', 'bacon', 'sausage', 'spam']>>> list(values)[2, 1, 1, 500]>>> # view objects are dynamic and reflect dict changes>>> del dishes['eggs']>>> del dishes['sausage']>>> list(keys)['spam', 'bacon']>>> # set operations>>> keys & {'eggs', 'bacon', 'salad'}{'bacon'}