user3141977
user3141977

Reputation:

If i import exit() from sys, which exit will it run?

In python if i import the exit module from sys will it run the normal exit() or sys.exit()

from sys import exit
print "Bla bla bla"
exit()

Upvotes: 5

Views: 10164

Answers (1)

phihag
phihag

Reputation: 287825

from a import b assigns the variable named b to the imported object, so you're overshadowing the built-in exit.

You can also just print out a string representation of exit to see this. Run

print(exit, type(exit))
from sys import exit
print(exit, type(exit))

to see:

(Use exit() or Ctrl-D (i.e. EOF) to exit, <class 'site.Quitter'>)
(<built-in function exit>, <type 'builtin_function_or_method'>)

Of course, there is rarely a reason to overshadow a built-in. Why not

import sys
sys.exit()

instead?

Upvotes: 6

Related Questions