On Sun, 16 Feb 2014 22:28:23 -0500, Roy Smith wrote: >> So when does code become data? When it's represented by an object. > > OK, now take somebody who knows lisp and try to explain to him or her > why Python's eval() doesn't mean data is code. Yeah, I know that's > pushing things a bit, but I'm trying to point out that people come into > things with pre-conceived notions that are hard to shake (the psychology > of learning people would call this the Law of Primacy).
There are ways to treat code as values: - you can use a string (an object/value) representing source code; - you can create a code object using compile(), passing it a string; - you can eval or exec on a string or a code object; - you can extract bits and pieces of function objects; and possibly others. But, and I think this is critical, you can't evaluate source code directly in Python. You can only do so indirectly, after creating some sort of object: a string, a code object, a function, etc. There is always an intermediate step: first, create a string object, then treat it as code. There's no functionality in Python for taking source code directly *as source code* and treating it as a value: function(import this) doesn't work, because `import this` is not a value that can be passed to the function. You have to make it a string first: function("import this") The Python compiler can check the syntax of actual source code at compile time, but it can't do anything about mock source code until runtime when you pass it to exec, eval or compile. That's because until that moment, it's just a string of characters, not source code. -- Steven -- https://mail.python.org/mailman/listinfo/python-list