Python: post #2263798 — TG.ME

razorblade23An no, the get_session is not running the whole time, yield returnes the result and stops execution of function You should learn about yield and how it works to understand get_session.
see from this a docs i found this, and the things is, when you say the fun which do yield somethings, this time it returns a value based on each time it call the fun, but the fun not executes, rather the loop works and return values one by one, like this in case of yield,

def give_date(
sdate: date,
edate: date,
) -> Iterator[date]:

delta = edate - sdate
print("This is executing")
for i in range(delta.days + 1):
day = sdate + timedelta(days=i)
print("Today is", day)
yield day

in this case when i call this fun each time it returns one day then another like this, so still the for loop works continuously right, i think yes?

but when i came to see the code there,
def get_session():
with Session(engine) as session:
yield session

i found here is not any loop or iterator so how it send new session each time after one yield session has done, next time what happens, what iterator it get and return what session value?
👍4
August 25, 2026