How To Execute Code AFTER a Return Statement in Python

preview_player
Показать описание
Usually in a function, nothing happens after a 'return' statement. But with some magic, we can force stuff to happen even after a return statement executes.

My coding setup:

Cheers
Рекомендации по теме
Комментарии
Автор

I did some tinkering with modifying the return value in the finally block. Integers, and presumably other immutable types, are not modified. Adding a key/value pair to a dictionary did result in the returned dictionary having that pair. Furthermore I used threading.Timer() to delay the adding of the pair. The initially returned dictionary did not have the pair but after the timer period, the dictionary did contain the pair.

I have no idea if there are any real world applications for this but it was fun to discover.

pschweitzer
Автор

This is actually very useful thank you

trp
Автор

After executing code at 3:50 the expected op should be:
1
2
#text_in_finally_block
hello

but in your op, hello is missing.... why it is not read by compiler even if it has been returned by the method?

anonymous
Автор

The reason why you never ever ever use a 'bare' except'! A program has the potential of never exiting if you use a bare except. Return statements are exit exception events with code zero, more specifically "basic exception system events" of which the bare exception clause catches most of them. If you didn't understand, the lesson is: Never never never ever do this!

Andrumen
Автор

i always feel sleepy after typing time.sleep like the code comanded me to sleep

Doggy_Styles_Coding