Wednesday, March 25, 2015

WLST / Python: execfile versus import

I have lately come across to some usage of "execfile".

I had immediately the impression "this stinks, why don't they use import instead?"
I also read here that execfile is stinky-stinky.

Let's see if they are actually equivalent.
def hello():
  print 'helloooo!'

To use the method I run the and type:

ok cool, so execfile has the effect of "inlining" your code. This is very 1960-programming style. All is just a big ball of mud.

If instead of execfile I use import:
import toimport

NameError: hello


the apparent advantage is that the method is QUALIFIED by its module. Disadvantage is that you must type the extra module name.

The "better" way (IMHO) is to explicitly import the method:
from toimport import hello


Needless to say, one should strive for true OO programming, so that the method hello() is defined for a specific class of objects... so please steer away from this "static method compulsive programming syndrome (SMCPS)".

PS execfile is deprecated in Python 3... so just get used to NOT using it...

No comments: