2

Got one looping script that run by itself, now I want to add another script inside the first, I've inserted a var at the start of first file, if it's true then load the second module. It work,but I'm wondering if his is a good practice?

if abilitaPippo == True:
 try:
 import Pippo
 Pippoabilitato = True
 except ImportError:
 Pippoabilitato = False
else:
 Pippoabilitato = False
Markku K.
3,94622 silver badges21 bronze badges
asked Nov 15, 2013 at 20:17
6
  • no. terrible. use functions. Commented Nov 15, 2013 at 20:25
  • Please clarify what you mean by "add another script inside the first" Commented Nov 15, 2013 at 20:27
  • maybe help: stackoverflow.com/questions/5027352/… Commented Nov 15, 2013 at 20:32
  • I don't see any issues with that. Although it is easier to maintain the code when all imports are in one place (typically at the begining of the file). However that's not mandatory. Commented Nov 15, 2013 at 20:36
  • 2
    Doing if abilitaPippo: is the same as if abilitaPippo == True:. The == True does nothing. Commented Nov 15, 2013 at 20:44

2 Answers 2

1

Python modules which 'do things' at global scope become fragile because they work differently when run directly to when they are imported. Also within a Python process a module will only be 'executed' once - on the first import. After that, import detects it's already been loaded and doesn't need to do anything.

Have a look at this: http://plope.com/Members/chrism/import_time_side_effects

This avoidance of side-effects is also the reason for the typical Python idiom

if __name__ == '__main__':
 main()

which you will often in see in scripts run from the command line. When run from the command line, the __name__ global variable is the string 'main', but when a module is imported, __name__ is the name of the module, so nothing is run directly.

answered Nov 15, 2013 at 21:05
Sign up to request clarification or add additional context in comments.

Comments

1

Python is not C with #ifdef and so on and, as such, you should avoid conditional inclusion of code. Or to put it differently: you can do this but it's not recommended practise in Python.

Instead you should figure out what the code should do and then write it based on that. For example, you can pass command-line flags to your script and let the runtime behaviour vary depending on which command-line flags were passed.

That having been said in Python is common to have the following for modules that may or may not be installed:

try:
 import mymodule
except ImportError:
 pass
answered Nov 15, 2013 at 20:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.