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:

On Sat, Dec 23, 2017, 09:23 William Rose, <william27.07.02@gmail.com> wrote:
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 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:
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:
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).

On Sat, Dec 23, 2017, 09:23 William Rose, <william27.07.02@gmail.com> wrote:
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 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:
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:
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