Python中没有任何内容在当前块的末尾结束。 它们只以范围退出结束。删除顺序 未定义,您也可以更改它。 您的方法不允许以下 (互斥锁): increment_counter() x = counter() $ b带锁定的$ b(互斥锁): decrement_counter() 除了制作一个新的块,因为 如果1: 锁定(互斥锁) x = counter() 如果1: 锁定(互斥锁) 如果块的数量有问题,那就不好了。 很难做到 与multi(锁定(someMutex), 开盘(readFilename), 开盘(writeFilename))为_,输入,输出: ...... 未经测试的草图实现 class multi(object): def __init __(self,* args) : self.args = args def __enter __(self): results = [] for i ,枚举中的arg(self.ar gs): 试试: results.append(arg .__输入__()) 除了: #通过已经__entered__ args支持 exc = sys.exc_info() 范围内的j(i-1,-1,-1): 试试: self.args [j] .__退出__(* exc) 除了: #需要新的异常,匹配PEP行为 exc = sys.exc_info() 提高exc [0],exc [1],exc [2] 返回结果 def __exit __(self,type,value,traceback): for self.args [:: - 1]中的arg: 试试: arg .__退出__(类型,值,追溯) 除了: 类型,值, traceback = sys.exc_info() Andrew da *** @ dalkescientific.comNothing in Python ends at the end of the current block.They only end with the scope exits. The order of deletionis not defined, and you would change that as well.Your approach wouldn''t allow the followingwith locking(mutex):increment_counter()x = counter()with locking(mutex):decrement_counter()except by making a new block, asif 1:locking(mutex)x = counter()if 1:locking(mutex)If the number of blocks is a problem it wouldn''t be thathard to dowith multi( locking(someMutex),opening(readFilename),opening(writeFilename) ) as _, input, output:...Untested sketch of an implementationclass multi(object):def __init__(self, *args):self.args = argsdef __enter__(self):results = []for i, arg in enumerate(self.args):try:results.append(arg.__enter__())except:# back up through the already __entered__ argsexc = sys.exc_info()for j in range(i-1, -1, -1):try:self.args[j].__exit__(*exc)except:# Need to get the new exception, to match the PEP behaviorexc = sys.exc_info()raise exc[0], exc[1], exc[2]return resultsdef __exit__(self, type, value, traceback):for arg in self.args[::-1]:try:arg.__exit__(type, value, traceback)except:type, value, traceback = sys.exc_info()Andrew da***@dalkescientific.com 删除顺序没有变化,它''只是定义了对__exit__的调用的 顺序,它们完全相同。据我所知,PEP343与删除顺序无关,这仍然是依赖于实现的。它不是像C ++ RAII那样的构造函数/析构函数,而是__enter __ / __ exit__。 但是,它通过创建一个先例创造了一个先例声明影响当前缩进块的结尾 。但这就是这个PEP的全部内容 关于... 你的方法不允许以下 不,我说'':''*可选*。我完全同意支持'':'' 有用。 如果块的数量有问题,那就不难了 用multi(锁定(someMutex),打开(readFilename),打开(writeFilename))为_,输入,输出: ......There''s no change in order of deletion, it''s just about defining theorder of calls to __exit__, and they are exactly the same. As far as Iknow, PEP343 has nothing to do with order of deletion, which is stillimplementation-dependant. It''s not a constructor/destructor thing likein C++ RAII, but __enter__/__exit__.But yes, it creates a precedent by creating a statement affecting theend of the current indentation block. But that''s what this PEP is allabout... Your approach wouldn''t allow the followingNo, I said making the '':'' *optional*. I totally agree supporting '':'' isuseful. If the number of blocks is a problem it wouldn''t be that hard to do with multi( locking(someMutex), opening(readFilename), opening(writeFilename) ) as _, input, output: ... True。但它看起来一样好吗?特别是_部分? 问候, NicolasTrue. But does it look as good? Particularly the _ part?Regards,Nicolas 这篇关于评论:PEP 343:匿名块Redux和Generator增强功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 09:08
查看更多