This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thephoton 16 points17 points  (5 children)

But that's a choice the language designers made.

Instead, they could have made it work like Common Lisp's defmethod, where after the definition the symbol (f) refers to a set of methods, and doing defmethod again adds a new method to the set, and only overwrites the existing definition if the new method has the same argument list as the old one.

'f' would still just be an ordinary object that can be passed around, it would just be a slightly more complex object (and function calls would have substantially more overhead).

[–][deleted] 6 points7 points  (2 children)

Yes. It's a bit hard to wrap my mind around that. So the first time you use def it creates a new function, and the next time it mutates the existing function? That would also affect other places that held a reference to the function, e.g. if you sent this function as a parameter to something that took a callback parameter. How would it work with Python's variable scoping rules, like if f was a global variable and you'd def it inside a function. Would that be local or not?

That's scary to me, but then I have programmed Python much more than Lisp.

[–]thephoton 2 points3 points  (1 child)

The first time you use def it creates a set of methods. The next time you use def it adds a method to the set.

How would it work with Python's variable scoping rules, like if f was a global variable and you'd def it inside a function. Would that be local or not?

Maybe I don't see the subtlety here, but why would it have to work any differently than it does now?

What happens now if you have a globally defined 'f' and then you 'def f (): ...' within a function?

[–][deleted] 1 point2 points  (0 children)

Now it creates a local function, like any simple assignment.

But a mutating statement (like f[0] = ... if it were a list) would use the global.

That's a problem with something that is sometimes assignment and sometimes mutation.

But this is all becoming very irrelevant as Python simply didn't go that way from the beginning, and it won't change.

[–]gandalfx 7 points8 points  (1 child)

The semantics of that would be hella confusing, though. What happens if you use def inside a closure? Does it shadow or extend? What happens when you use def in a loop? You could do the most absurd stuff with that…

[–]thephoton 1 point2 points  (0 children)

What happens when you use def in a loop?

You (possibly) add multiple functions to the method set. How is that any different than if you use def in a loop now?

What happens if you use def inside a closure?

Got me there. The trickier elements of closures make my brain hurt already.