Re: [Python-ideas] Internal function idea
[removing Python-ideas-owner so any replies don't flood my inbox] On Sat, Dec 23, 2017, 09:23 William Rose, <william27.07.02@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!
On Sat, Dec 23, 2017, 09:23 William Rose, <william27.07.02@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?
On Sat, Dec 23, 2017, 09:23 William Rose, <william27.07.02@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!
I suspect you misunderstand how variables (actually, name bindings) work in Python. If you do understand, I don't understand what you're guarding against. With current semantics, code inside a function cannot change a global binding, although it can refer to one:
def f(): ... x=3 ... return x ... def g(): ... return x ... x = 0 y = f() # Changes x? z = g() print("x =", x, "y =", y, "z =", z) x = 0 y = 3 z = 0 # Nope. def h(): ... y = x + 5 ... x = y # Changes x? ... return x ... w = h() # Nope, it crashes your program instead! Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in h UnboundLocalError: local variable 'x' referenced before assignment
You *can* use the global and nonlocal declarations to override the normal automatic assumption that names are local to the scope in which they are bound:
def a(): ... global x ... x = 42 ... return x ... a() 42 x 42
Prohibiting this last would be un-Pythonic, IMO (violates the "consenting adults" principle). Similarly for class variables, which can only be accessed using attribute notation. There are also conventions, prefixing "_" or "__", which indicate "this is private, mess with it at your own risk", and actually munge the name internally to make it impossible to access accidentally (including in derived classes).
participants (3)
-
Brett Cannon
-
Franklin? Lee
-
Stephen J. Turnbull