Python learning notes (the next day) and python learning notes
1. dictionary replication:
Dict = {'name': 'wang', 'sex': 'M', 'age': 34, 'job': 'it '}
Info = dict # Alias (the two dictionaries point to the same address space in the memory)
Info1 = dict. copy () # shadow copy (the first layer of the nested dictionary is independent and associated below the second layer)
Import copy
Copy. copy () # shadow copy
Copy. deepcopy () # deep copy (completely independent)
Note: The associations in the light replication mode are only for the nested objects contained in the dictionary's initial state. Newly Added objects will not
Example:
>>> Dict
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'sex': 'M', 'age ': 40, 'name': 'wang '}
>>> Dict_alias = dict
>>> Dict_copy = copy. copy (dict)
>>> Dict_deep = copy. deepcopy (dict)
# Adding, changing, and deleting the first-level object key values are not affected in light replication and deep Replication
>>> Dict ['age'] = 32
>>> Del dict ['sex']
>>> Dict
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_alias
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_copy
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
>>> Dict_deep
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
# Change or delete the original second-level object key value. The shortest copy is affected, but the deep copy is not affected.
> Dict ['info'] [2] = 100
>>> Dict
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_alias
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 32, 'name ': 'wang '}
>>> Dict_copy
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
>>> Dict_deep
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
# Adding second-level objects will not be affected in light replication and deep Replication
>>> Dict ['new'] = {'A': 1, 'B': 2, 'C': 5}
>>> Dict
{'Info': ['A', 'B', 100, 2], 'name': 'wang', 'age': 32, 'job ': 'it', 'new': {'A': 1, 'C': 5, 'B': 2 }}
>>> Dict_alias
{'Info': ['A', 'B', 100, 2], 'name': 'wang', 'age': 32, 'job ': 'it', 'new': {'A': 1, 'C': 5, 'B': 2 }}
>>> Dict_copy
{'Info': ['A', 'B', 100, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
>>> Dict_deep
{'Info': ['A', 'B', 1, 2], 'job': 'it', 'age': 40, 'name ': 'wang ', 'sex': 'M '}
2. built-in function description:
_ Name __: the main file is returned; otherwise, the file name is returned, which can be used to determine whether the main file or the import module is used;
_ File __: absolute file path;
_ Doc __: description of the beginning of the file
Example:
'''
Created by 2015-12-18
@ Author: kevin
'''
If _ name _ = '_ main __':
Print ('this is main file ')
Print (_ file __)
Print (_ doc __)