On Thu, Aug 02, 2018 at 03:13:25PM +0200, Robert Vanden Eynde wrote:

> This brings the discussion of variable assignement in Expression. Functional
> programming community seems to be more interested in python.

I'm not sure what you mean there. Your English grammar is just slightly 
off, enough to make your meaning unclear, sorry.


> lines = (f.readlines() with open('hello') as f)

readlines has the same problems as read, as I described earlier, and the 
same trivial three-line solution.


> digit = (int('hello') except ValueError: 5)

try...except exceptions have been proposed before and rejected.


> value = (x+y**2 where x,y = (2,4))

A "where" *statement* is interesting, but this is not a good example of 
it. The above is better written in the normal syntax:

    value = 2 + 4**2

no need to introduce temporary variables that exist only to obfuscate 
the code.


> values = [x+y**2 for x in range(5) for y in range(7)]
> values = [x+y**2 for x,y in product (range(5), range(7))]
> y = 5 if condition else 2

These already exist, because they are useful.


> y = (lambda x: x+2)(x=5)

This is not a good example of the use of a lambda. Better:

    y = 5 + 2

Why bother writing a function with such a simple body if you are going 
to immediately call it on the spot? Unless the body is more complex, or 
you are going to call it elsewhere, or call it repeatedly, the lambda 
adds nothing.

Nobody denies that *some* statements are well-suited and useful as 
expressions. The question is whether "with" is one of those.


> with open('hello') as f:
>     lines = f.readlines()
> del f  # f is leaked !

99% of the time, I would think that "del f" was a waste of time. If that 
code is in function, then f will be closed when the "with" block is 
exited, and garbage collected when the function returns.

If you are worried about the memory efficiency of one tiny closed file 
object, then Python is the wrong language for you.

If that code is in the top-level of a script, who cares about f? You 
surely don't delete all your variables when you are done with them:

    name = input("what is your name?")
    print("Hello,", name)
    del name
    play_game()


The only time I would explicitly delete f like you do above was if I was 
writing a library which specifically and explicitly supported the "from 
module import *" syntax, AND there were too many exportable names to 
list them all in __all__ by hand. Only in that very rare case would I 
care about tidying up the global namespace by using del.


> x,y = 2,4
> value = x+y**2
> del x, y  # x,y are leaked !

If you are writing code like this, you are just obscuring the code. Much 
better to just use the values where you need them, not to invent 
unnecessary temporary variables that you don't need!

    value = 2 + 4**2


[...]
> If we add one, it's Logical to add the others to be consistent.

My car has round wheels. Since we use one shape (circle) for wheels, it 
must be "logical" to make wheels in all other shapes, to be consistent:

- triangular wheels
- square wheels

etc. Consistency just for the sake of consistency is *not* a virtue. 
Unless those expression forms justify *themselves* on their own merits, 
it is just a waste of time and effort to let them sneak into the 
language "for consistency".

 
> Of course, one can always write functions like read_text but the ide of
> those construction is like the lambda, we want anonymous.

We can say:

    text = read('filename')

    text = f.read() with open('filename') as f

and both are equally unanonymous (both use a named variable), or we can 
say:

    process(spam, eggs, read('filename'), foo, bar)

    process(spam, eggs, f.read with open('filename') as f, foo, bar)

and both are equally anonymous.

If Python had a built-in function "read", surely you wouldn't refuse to 
use it because it was a named function? We don't complain that map() and 
filter() are named functions and demand "anonymous" ways to do the same 
thing. A read function should be no different.

The only point of difference is that it is not built-in, you have to 
write it yourself. But not every trivial three-line function must be 
built-in.



-- 
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to