with Tag('这就是一个简单说明', will_run=True): codes...
就是这样will_run
是 False
的话, 后面的代码就不执行,或者执行但是无效。
我想不出怎么用 __init__
, __enter__
, __exit__
搞定
所以来问一下大家。
我 1 楼的思路,测试下来被打脸了:
In [1]: class Tag(object): ...: def __init__(self, comment, will_run=True): ...: self.will_run = will_run ...: def __enter__(self): ...: if not self.will_run: ...: return ...: def __exit__(self, *exec_info): pass ...: In [2]: with Tag('这是一个实验', will_run=True): ...: print(123456) ...: 123456 In [3]: with Tag('这是一个实验', will_run=False): ...: print(123456) ...: 123456
请教这要怎么搞?
![]() | 1 northisland OP ``` class Tag(object): def __init__(self, comment, will_run=True): self.will_run = will_run def __enter__(self): if self.will_run==False: return def __exit__(self, *exec_info): pass ``` 是这样么?我去执行试试~~对 enter 理解不深入。 |
![]() | 2 laobubu 2018-10-29 17:50:56 +08:00 __enter__ 应该是不能控制 with 后面的块的吧。我能想到的就是 raise 个异常,让后面的代码全部都不能跑。 说回来,我觉得优雅点的做法可以 with Tag("xxx") as tag: 然后下一行就是 if tag.will_run: |
![]() | 3 misaka19000 2018-10-29 17:55:15 +08:00 感觉应该做不到 |
4 lolizeppelin 2018-10-29 18:00:15 +08:00 via Android enter 里直接 raise |
5 lolizeppelin 2018-10-29 18:00:52 +08:00 via Android 有需要再套一层不就得了 |
![]() | 6 Trim21 2018-10-29 18:06:42 +08:00 ![]() |
![]() | 7 lance6716 2018-10-29 19:01:09 +08:00 运行过程不会影响其他过程,除非抛异常 |
![]() | 8 Wincer 2018-10-29 19:14:16 +08:00 ```python from contextlib import contextmanager @contextmanager def test(will_run=True): if will_run: yield 233 else: return "" try: with test(will_run=False) as q: print(1234) except RuntimeError: pass ``` |