-1
class Test:
    def __init__(self):
        pass

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass


with Test():
    print (1) # any way to ignore it?

Is there a way ignore (not to execute) with statement body?

That is in this case I don't want print (1) to be executed (but I don’t want to wrap this expression in a function, etc.)

I need this for one personal experimental project.

Tried some options, like throwing exceptions, but it doesn't seem to work.

PS: I cannot wrap with body in any constructions (at least until the Test class is called). I would like to implement this inside the Test class, for example, when calling a class, some monkeypatch may work and so on.

PS2: the program should still work after ignoring a with body

me2 beats
  • 774
  • 1
  • 8
  • 24

2 Answers2

1
from contextlib import suppress

class Pass(Exception):
    ...

class _skip:
    def __enter__(self):
        raise Pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass

suppress = suppress(Pass)
skip = _skip()

with suppress, skip:
    print('ABC')
    raise SyntaxError
    print('XYZ')

print('Hello World')

You may not like the with suppress, skip: syntax exactly but I highly believe that they can be wrapped into a single manager, it is just that I'm too lazy to do it and this seems good enough.

Işık Kaplan
  • 2,815
  • 2
  • 13
  • 28
  • This is an interesting solution, it does not even use `sys.settrace`. It may be useful n my case, And yes, I would not want to have `suppress` in the `with` line, it seems this cannot be avoided in this case without some hard tricks... I think this is the only moment that stops me from accepting this answer. – me2 beats Sep 15 '19 at 21:29
  • Yeah I thought that you could just rewrite the `contextlib.nested` from py2 with `contextlib.ExitStack` but never thought that `_skip` is not actually a proper context manager. With that being said, yeah I don't think it would be possible to combine them without a hack. I don't really enjoy using code that depends entirely on runtime like `sys.settrace`, although if you don't mind it I can try to throw some magic into this and do it like `with skip:`. Although apparently someone already linked you something that's works with that kind of hack. – Işık Kaplan Sep 16 '19 at 12:01
  • if you mean hack it using settrace I think I don't need it because there is this solution https://stackoverflow.com/a/54765496/8324477. – me2 beats Sep 16 '19 at 12:29
0

You could just use an if False statement to wrap the body of what you don't want to execute, like so:

class Test:
    def __init__(self):
        print('>>', 'init')
        pass

    def __enter__(self):
        print('>>', 'enter with')
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('>>', 'exit with')
        pass


with Test():
    if False:  # will never run this block
        print (1) 

Run result:

>> init
>> enter with
>> exit with
wjandrea
  • 28,235
  • 9
  • 60
  • 81
sal
  • 3,515
  • 1
  • 10
  • 21
  • Sorry for the inaccuracy, I've supplemented the question.. I cannot wrap with body in any constructions – me2 beats Sep 15 '19 at 02:22