3

I am trying to run following simple code

import sys

print("Starting Test Python Module");

def testmethod():
    print("From test method")

sys.exitfunc = testmethod
print("Terminating Test Python Module");

and it prints

C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module

I am not able to understand why it does not print "From Test method"

Using atexit works fine though

import atexit

print("Starting Test Python Module");

def testmethod():
    print("From test method")

atexit.register(testmethod)
print("Terminating Test Python Module");

Outputs

C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module
From test method

2 Answers 2

6

sys.exitfunc is deprecated since python2.4 and was removed in python3.

Sign up to request clarification or add additional context in comments.

Comments

3

sys.exitfunc does not exist in Python 3.x.

3 Comments

Yes I am using Python 3.4.3. So this would explain the behavior. But shouldn't python interpreter complaint about it?
The protection against arbitrary attributes being added to objects of types defined in C apparently doesn't apply to modules. Should it? Possibly.
@AniketThakur No, you are simply setting an attribute exitfunc to sys module, there is nothing wrong about that. You can do sys.blahblahblah = <something> , and it would work ( as long as something is defined :-) , like it works for most other objects in python) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.