[Python-ideas] Internal function idea

Franklin? Lee leewangzhong+python at gmail.com
Wed Dec 27 00:06:28 EST 2017


On Sat, Dec 23, 2017, 09:23 William Rose, <william27.07.02 at gmail.com> wrote:
>
> I had an idea that it could be helpful to have local functions as well as
> normal ones. They would be called the same way as normal ones but def would
> be replaced by internal and inside they could only access variables they
> have defined and inputs to them so no global variables or class variables. I
> think this could be used to save people accidentally changing variables you
> dont' want to change when updating your code. Let me know what you think!

You mean like this?

internal myfunc(x, y, z):
   return sum(map(int, [x,y,z]))  # SyntaxError: Undefined name 'sum'.

You may want to loosen the restrictions and allow builtins. However,
it is possible to redefine/create builtin names during runtime.
You may also want to allow explicit declarations for global/nonlocal
names, using the global and nonlocal keywords.
You won't be able to access class variables, because you won't be able
to access classes.

This kind of function prevents a common use for functions: taking a
section of an existing function and giving it a name. The proposed
`internal` function type will encourage large functions that break the
Rule of Three*, and require people to opt in to gain any advantages.
Once they opt in, they would have to then opt out if they try to apply
the Rule of Three.

*https://en.wikipedia.org/wiki/Rule_of_three_%28computer_programming%29

Can you give an example of how you would use this? Could your problem
perhaps be better solved with namespaces or refactoring tools?


More information about the Python-ideas mailing list