coding style - where to declare variables

Chris Angelico rosuav at gmail.com
Sun Jul 22 16:38:27 EDT 2018


On Mon, Jul 23, 2018 at 6:14 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> While FORTRAN or C couldn't operate on functions like this, an assembly
> language program could easily. Simply compose a CPU instruction sequence
> on the fly, mark it executable and use the "CALL" opcode to transfer
> control to your constructed function.

... right up until the point where you realize that this is a massive
security vulnerability, so you get a segmentation fault (or
"protection fault" under Windows) for trying to execute a
non-executable segment.

> In the same vein, you could understand the "def" statement as a runtime
> compiler that takes the function body, compiles it into machine language
> and assigns the start address to the given variable. In fact, that would
> be a perfectly working way to implement "def." Whether it would be a
> smart thing to do is a different question. Key is, though, that "def"
> always creates a new *data* object that can be called as *executable
> code*.

Here's a simpler way to describe it:

The 'def' statement creates a function and assigns it to the name given.

Tada! No need to talk about compilers and addresses and stuff, which
are utterly irrelevant in Python.

ChrisA



More information about the Python-list mailing list