with-statement

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

走远了吗. 提交于 2021-01-20 17:18:07
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

时间秒杀一切 提交于 2021-01-20 17:18:07
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

VBA - Nested “With Statements” within “IF Statements”

[亡魂溺海] 提交于 2021-01-04 07:35:07
问题 Language: VBA - MS Access I am using User-Defined-Types (UDT) within my code. I would like to be able determine which section of the UDT i'm loading data into based on a state variable. My first attempt was to use "With" statements nested into an "IF" statement. This doesn't work (I get a compiler error that says Else without if). Is there a way to make this work? or another way of going about using a state variable to determine which section of the UDT i'm loading? Type MyOtherType Name as

Purpose of using “with tf.Session()”?

删除回忆录丶 提交于 2020-12-29 12:23:53
问题 I am practicing the keras method called concatenate. And use of with statement in this example kind of got me thinking the purpose of this statement The example code looks like: import numpy as np import keras.backend as K import tensorflow as tf t1 = K.variable(np.array([ [[1, 2], [2, 3]], [[4, 4], [5, 3]]])) t2 = K.variable(np.array([[[7, 4], [8, 4]], [[2, 10], [15, 11]]])) d0 = K.concatenate([t1 , t2] , axis=-2) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run

python lock with-statement and timeout

此生再无相见时 提交于 2020-12-28 18:26:15
问题 I am using a Python 3 sequence like this: lock = threading.Lock() res = lock.acquire(timeout=10) if res: # do something .... lock.release() else: # do something else ... I would prefer to use a with-statement instead of explicit "acquire" and "release", but I don't know how to get the timeout effect. 回答1: You can do this pretty easily with a context manager: import threading from contextlib import contextmanager @contextmanager def acquire_timeout(lock, timeout): result = lock.acquire(timeout

python lock with-statement and timeout

别说谁变了你拦得住时间么 提交于 2020-12-28 18:25:23
问题 I am using a Python 3 sequence like this: lock = threading.Lock() res = lock.acquire(timeout=10) if res: # do something .... lock.release() else: # do something else ... I would prefer to use a with-statement instead of explicit "acquire" and "release", but I don't know how to get the timeout effect. 回答1: You can do this pretty easily with a context manager: import threading from contextlib import contextmanager @contextmanager def acquire_timeout(lock, timeout): result = lock.acquire(timeout

Is it safe to combine 'with' and 'yield' in python?

倖福魔咒の 提交于 2020-11-30 04:32:10
问题 It's a common idiom in python to use context manager to automatically close files: with open('filename') as my_file: # do something with my_file # my_file gets automatically closed after exiting 'with' block Now I want to read contents of several files. Consumer of the data does not know or care if data comes from files or not-files. It does not want to check if the objects it received can be open or not. It just wants to get something to read lines from. So I create an iterator like this:

Is it safe to combine 'with' and 'yield' in python?

只愿长相守 提交于 2020-11-30 04:32:01
问题 It's a common idiom in python to use context manager to automatically close files: with open('filename') as my_file: # do something with my_file # my_file gets automatically closed after exiting 'with' block Now I want to read contents of several files. Consumer of the data does not know or care if data comes from files or not-files. It does not want to check if the objects it received can be open or not. It just wants to get something to read lines from. So I create an iterator like this:

Can I use pymysql.connect() with “with” statement?

我的梦境 提交于 2020-08-24 05:39:51
问题 The following is listed as example in pymysql: conn = pymysql.connect(...) with conn.cursor() as cursor: cursor.execute(...) ... conn.close() Can I use the following instead, or will this leave a lingering connection? (it executes successfully) import pymysql with pymysql.connect(...) as cursor: cursor.execute('show tables') (python 3, latest pymysql) 回答1: This does not look safe, if you look here, the __enter__ and __exit__ functions are what are called in a with clause. For the pymysql