SoFunction
Updated on 2024-12-17

python basic learning generator and file system knowledge summary

I. Generator

First explain the iterator.

The ability to iterate over sequences (such as objects in a list or lines in a file) in a consistent way is an important feature of Python. This is accomplished through something called the iterator protocol, which is a generalized way of making objects iterable.

present, a native method for making objects iterable.

some_dict = {'a': 1, 'b': 2, 'c': 3}  
for key in some_dict:
    print(key)
#Output:
a
c
b

An iterator is a special object that can deliver objects to the Python interpreter in contexts such as for loops. Most methods that can accept objects like lists can also accept any iterable object. Examples include built-in methods like min, max, sum, and type constructors like list and tuple.

dict_iterator=iter(some_dict)
print(dict_iterator)
print(list(dict_iterator))
# Output
<dictionary-keyiterator object at 0x7ff0105bea48>
['a', 'c', 'b']

A generator is a simple way to construct new iterable objects.. While normal functions return a single value after execution, generators return a sequence of values in a delayed manner, i.e., they pause after each value is returned and continue until the next value is requested. To create a generator, simply replace return with yeild in the function.

def squares(n=10):
    print('Generating squares from 1 to {0}'.format(n ** 2))
    for i in range(1,n+1):
        yield i**2
gen=squares()# Note: When this generator is called, no code is executed immediately and the print operation is not executed!
print(gen)  #<generator object squares at 0x7f3a75af4b40>
 
# It won't start executing its code until you request elements from this generator
for x  in gen: 
    print(x,)

Output:

<generator object squares at 0x7f3a75af4a00>
Generating squares from 1 to 100
(1,)
(4,)
(9,)
(16,)
(25,)
(36,)
(49,)
(64,)
(81,)
(100,)

Another, more concise way to construct a generator is to use a generator expression. This is a type of generator that is similar to a list, dictionary, or collection derivation. Note thatIt is created by changing the square brackets at the ends of the list derivation to round brackets:

gen=(x**2 for x in range(5))
print(gen)
print(type(gen))
for i in gen:
    print(i)
exports:
<generator object <genexpr> at 0x7ff01066ef00>
<type 'generator'>
0
1
4
9
16
# is exactly equivalent to the much longer generator below:
def _make_gen():
    for x  in range(5):
        yield x**2 
 
gen=_make_gen()
for i in gen:
    print(i)

II. Documentation system

Use the built-in open function with the following parameters:

Commonly used documentation methods

# Use with statements to easily clean up open files and automatically close the file stream when the run is essentially finished

with open(path) as f: 
    lines=[() for x in f]

to this article on the python basic learning generator and file system knowledge summarizes the article is introduced to this, more related python generator and file system content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!