[Tutor] How does this work?

Daniel Yoo dyoo at WPI.EDU
Wed Feb 7 04:33:59 CET 2007



On Tue, 6 Feb 2007, Tony Cappellini wrote:

> this is the caller
> ##############################
> callee=open("tester.py").read()
> exec(callee)
> eval("main(['', 'argument'])")
>
> ##############################

> this is the callee which is saved in tester.py
> ##############################
> import sys
>
> def main(arg):
>   if arg != []:
>      print"\nArgument is %s" % arg
>
> if __name__ == "__main__"":
>   main(sys.argv)
> ##############################

Hi Tony,

Ack!

This is not safe.  I would strongly recommend not to do this.  There is a 
much simpler way for the caller to be written:

############################
import tester
tester.main([], "argument")
############################

Done.  No tricks, no eval() or exec() necessary.


> Is this a useful thing to do, or bad in practice?

Very Bad to do in practice.  It seems to be deliberately trying to be 
obfuscated.


Best of wishes!


More information about the Tutor mailing list