Re: Syntax Suggestion: Pass Function Definition as Argument

2019-11-08 Thread Stephen Waldron
Ok firstly, this idea was inspired specifically by a project I'm working on for 
school concerning linked lists, in which I was trying to create a method that 
performed a function on elements iteratively without having to navigate the 
list from the head each time (of course taking the function as a parameter). 
This project is intended to be a personal module for future use. 

In the process of this discourse here, however, I think I've found an 
alternative for this specific issue, (so thanks guys) but generally speaking, 
there were other times I've wanted to use functions as parameters, typically 
while making personal modules, like a purely Js button module I had made a few 
years back, and it was apparent that the only way to do so in Python was to 
define first, then pass, which I simply didn't like the idea of in the context 
of a module, short of defining each function before the module instances with 
`def temp(*args):` or some other generic name I won't use past the module 
function instance.

Now yes, as many of you have pointed out, there may exist other workarounds for 
various instances in which one may think to use a function as an argument. 
Multi-line lambdas are a very intriguing method for the broad topic, (thanks, 
they will be useful in the future ^ ^b ++), but as @Chris Angelico mentioned, 
they can't really be used as replacements for def, and from what I've 
experienced trying them out, it wasn't always clear what could be done for some 
of the lines one would want to write.

Finally, some clarification for @David Raymond
> Isn't this basically just defining a function... but without an argument 
> list? How would that know what "str" is?

In my original post, I said:
> If they are not, they won't be assigned to, which would cause an error unless 
> a default value is given. These parameters could be available for passing in 
> the annexed function if mentioned when the parameter is called.

Nyeh, it's written poorly, but I guess I should clarify by saying that, per the 
proposal, arguments are inherent from the parent function to the annexed 
function if and only if that argument is assigned to when the annexed function 
is called as the parameter it fills in the parent function's definition. Of 
course it can also use all other variables within it's scope.

In the example you're referring to, this is done on line 3:
foo (str = str1)
making foo inherit "str" from myFoo, which was given the default value " ".

(I would suppose another viable alternative would be using nonlocal as I'll 
example below)

Also you ask if it's just defining a function. In all of my examples, I use 
only one instance of the parent function (which is actually pretty useless), 
but the power in doing this actually lies in calling it in various locations 
and passing different arguments and annexing different functions (kinda what 
functions are for, haha). I might be hard-pressed to think of an example short 
of writing out a more complex program, but here is.. something?.. I guess 
simply assume runOpt(prompt, foo) is a previously defined function, and 
variables used in the annex are within the annexed function's scope, but prompt 
is not inherited:


runOpt ("Play Game"):
getUserData()
chr[0].id = 0
time = 0
score = 0

scene = 1


runOpt ("How To Play"):
nonlocal prompt

loadBg()
display(prompt)

scene = 2


runOpt ("Settings"):
checkDefaults()
chr[0].id = 5

scene = 3





N.B. on everything (also some TLs you could skip): 
This is just a suggestion, I think it would be nice for previously stated 
reasons (ease, aesthetics, function, yadda-yadda). I would still love to hear 
ways I could currently achieve things similar, as that is one of the things I 
look for from suggestions since "I don't know how to do x right now and have 
not previously found a solution" is implied, but one trigger I do have is a 
reply of "that is not the way it is" to a "this is how I think it should be" 
statement; doesn't give me any hint as to the merits or demerits of my thought, 
as flawed as it may indeed be, in logistics, implementation, practice etc. 
Can't really say anyone has done that quite yet, but just making it known, so 
"What you can try" and "The suggestion is impractical because..." responses are 
welcome. 

Also, one of the reasons for my suggestions is ease. The structure used is 
fairly common in Python, and thus fairly intuitive (I hypothesize at least). I 
know from experience that once you learn something, it becomes fairly easy and 
intuitive for you, but not necessarily for others and you sometimes forget the 
struggle you endured to get there. Of course I'm not saying others shouldn't 
have to learn to, but if there's a way to make it easier for others, I'd push 
it. Now I am still going to learn all about async, await, yield (which I think 
deserves more cred from what I can see so far), and lambda (cuz I wanna) but if 
I think learning and utili

Re: Syntax Suggestion: Pass Function Definition as Argument

2019-11-07 Thread Stephen Waldron
Thanks Antoon. I do suppose that it is kind of wrong to say the only way is to 
"reference its [the function's] name" as an argument, however the point I was 
trying to make was that it isn't possible to pass a function that is either not 
in some way previously defined or a reference to something previously defined.

In your example, you still make reference to the existing function `truediv` 
from the `operator` library, even though the `partial` function does create a 
new function.

What I'm aiming for is the ability to, within a function call, pass a suite 
that would be there automatically defined by the compiler/interpreter. Another 
comment did mention lambda functions, which does to some degree provide that 
capability, but is restricted to well, lambda functions (only expressions, not 
statements).

Your example does however suggest the possibilities of a function or expression 
that creates functions. For example, the `function` expression in JavaScript.

```
//Definition
function myFoo (str1, str2, foo){
console.log( foo(str1), foo(str2) );
}


//Call
myFoo ("hello", "world!", function(str){

str[0] = str[0].toUpper();
return str;

});


Output:
Hello World!
```

However it does not seem that there would be any such expression in Python as 
is. In python, this code would be written:


```
#Definition
def myFoo (str1, str2, foo):
print( foo(str1), foo(str2) )


#Call
def other_foo(str):
str = list(str)[0].upper() + str[1:]
return str

myFoo ("hello", "world!", other_foo)

```


Here is it rewritten using the proposal:
```
#Definition
def myFoo (str1, str2, foo, str = " "):
print( foo(str = str1), foo(str = str2) )


#Call
myFoo ("hello", "world!"):
str = list(str)[0].upper() + str[1:]
return str

```

Of course this example presents only a minor, though you can see the difference 
in the call, in which no new function needs be referenced.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Syntax Suggestion: Pass Function Definition as Argument

2019-11-07 Thread Stephen Waldron
Thanks Antoon. I do suppose that it is kind of wrong to say the only way is to 
"reference its [the function's] name" as an argument, however the point I was 
trying to make was that you cannot pass a function that is either not in some 
way previously defined or a reference to something previously defined.

In your example, you still make reference to the existing function `truediv` 
from the `operator` library, even though the `partial` function does create a 
new function.

What I'm aiming for is the ability to, within a function call, pass a suite 
that would be there automatically defined by the compiler/interpreter. Another 
comment did mention lambda functions, which does to some degree provide that 
capability, but is restricted to well, lambda functions (only expressions, not 
statements).

Your example does however suggest the possibilities of a function or expression 
that creates functions. For example, the `function` expression in JavaScript.

```
//Definition
function myFoo (str1, str2, foo){
console.log( foo(str1), foo(str2) );
}


//Call
myFoo ("hello", "world!", function(str){

str[0] = str[0].toUpper();
return str;

});


Output:
Hello World!
```

However it does not seem that there would be any such expression in Python as 
is. In python, this code would be written:


```
#Definition
def myFoo (str1, str2, foo):
print( foo(str1), foo(str2) )


#Call
def other_foo(str):
str = list(str)[0].upper() + str[1:]
return str

myFoo ("hello", "world!", other_foo)

```


Here is it rewritten using the proposal:
```
#Definition
def myFoo (str1, str2, foo, str = " "):
print( foo(str = str1), foo(str = str2) )


#Call
myFoo ("hello", "world!"):
str = list(str)[0].upper() + str[1:]
return str

```

Of course this example presents only a minor, though you can see the difference 
in the call, in which no new function needs be referenced.
-- 
https://mail.python.org/mailman/listinfo/python-list


Syntax Suggestion: Pass Function Definition as Argument

2019-11-07 Thread Stephen Waldron
Hi, I'm new to the group and to Python, so forgive me if I make any faux-pas 
here. As I can tell, the only way to pass a function as an argument is to 
reference its name as follows:

def foo1(message):
print(message)

def foo2(foo, message):
print("Your function says:")
foo(message)


>>foo2(foo1, "Hello World!")
Your function says:
Hello World!


This is how it is at the moment, however it may be more agreeable, especially 
if that is the only purpose of the function, for python users to be able to 
define new functions inside of function calls. The way I propose this may occur 
is using similar syntax as control structures, as exampled below:

def foo2(foo):
print("Message to print:", end = " ")
foo()


>>foo2():
print("Hello World!")

Message to print: Hello World!


In this proposal, it should be noted, that unlike control structures, the 
brackets are required for the function call. The "suite" or "annex" is defined 
as a function object by the compiler and passed inside the call. Here are some 
other examples of the proposal.



*Non-Function Arguments with Annexed Functions*
Non-Function Arguments should be placed in the brackets before annexed 
functions, and this should be reflected in the parameter order. If they are 
not, they won't be assigned to, which would cause an error unless a default 
value is given. These parameters could be available for passing in the annexed 
function if mentioned when the paramaeter is called.

def myFoo (num1, num2, foo):
return foo (num1, num2)


>>myFoo(20, 30):
return num1 + num2

50


*Annexing Multiple Functions or Out of Order Annex*
To specify the order in which functions are annexed, or annex multiple 
functions, one could use the parameter name before the specific suite.

class Book:
def __init__(self, name, author):
self.name = name
self.author = author


def doToBooks (bookList, first, then, book = None):

for book in bookList
first(book = book)
then(book = book)

myBooks = [
Book("The Way of the World", "Lucy Cole"),
Book("To Live or To Love", "Georgio Dunham"),
Book("Twelve Days of Success", "Anita Duvette")
]

doToBooks (myBooks):
print(book.name)
then:
print(" - " + book.author + "\n")



>
The Way of The World
 - Lucy Cole

To Live or To Die
 - Georgio Dunham

Twelve Days of Success
 - Anita Duvette



This proposal does not detract from the core appearance and appeal of Python 
while creating an option for users and module makers who want themselves or the 
users of their modules not to have to previously define functions in order to 
use the code. This would make such code more aesthetic and compact, and take 
away the need for defining a new function, name and all, specifically for such 
which ultimately would be used only once as an argument. 
   
-- 
https://mail.python.org/mailman/listinfo/python-list