News for hackers. Stuff that matters.
Re:PyPy gets funding from Mozilla for Python 3.5 support by nemetroid on Tuesday August 09, 2016 @03:31PM (#12257164)
Python 3.6 is adding `f"{foo}"`, which is rather different than the previous offerings.
Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Tuesday August 09, 2016 @03:45PM (#12257241)
Yes, but to clarify, it's only rather different than previous python offerings. There exist things more or less equivalent in other languages, so it's not as if there's much of a reason it took this long.
Re:PyPy gets funding from Mozilla for Python 3.5 support by nilved on Tuesday August 09, 2016 @03:49PM (#12257267)
Wow that is horrible.
Re:PyPy gets funding from Mozilla for Python 3.5 support by allendoerfer on Tuesday August 09, 2016 @05:30PM (#12257840)
I really hate the fact that there are now 3 ways to do it. And that it looks like PHP.

To be honest, to me the "%" seems the most pythonic way to do it. Format does not seem easier to understand to me . String formatting just cannot be transformed to natural language. I at least don't think "I need to format this string", I think "this variable needs to be in there", in the in there might as well be called "%". I think ["a is %s" % a for a in all_as] just fits.

Although named parameters are nicer with format. In no way should there be a third way to do it. Isn't that the basic principle of Python, to care about things like this? I mean this whole Python 3 problem was to a small extend caused by people rather having an incompatible syntax than two ways to print a string. Going through these troubles and then inflating the language with new syntax for the same thing seems like a waste.

Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Tuesday August 09, 2016 @06:16PM (#12258100)
In that respect, don't list comprehensions fall under the same category of multiple ways to do something? They didn't exist in the language until 2.0[1], and it's just a convenient way to encode a couple for loops, is it not[2]? At some point you have to accept that it's a trade-off, and the more complex the topic, the less well served it will be by only one or two ways to do it. In this case, there were actually already three ways to do it, since you could always use plain string concatenation with `+`. If python were truly about one way to do it more than anything else, that would be the only way to build complex strings from variables, but since convenience is also important, numerous shortcuts were deemed acceptable and implemented.

1: https://en.wikipedia.org/wiki/History_of_Python#Version_2.0

2: And for my money, significantly more confusing (and less powerful) than something like Perl's `map`.

Re:PyPy gets funding from Mozilla for Python 3.5 support by allendoerfer on Tuesday August 09, 2016 @06:39PM (#12258202)
List comprehension is the one obvious way to do do a quick map, a for loop would be used for more complex stuff and side effects. A real map is more niche but probably necessary when doing eval stuff or stylisticly nicer in a module where you do several functional tasks.

String concatenation has one use-case where it is the obvious way, too, namely when you have to concatenate already existing strings without modification, since "%s%s" % (a, b) is just stupid, beyond that you would use string formatting.

Maybe there are unique obvious use-cases for the different formatting options as well, but I don't know them. I give you, that "%" is bad for named arguments, but why wouldn't you just improve that then? I think they do the exact same thing and are good for the exact same contexts. If somebody knows more than me, I would be very happy to be proven wrong.

Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Tuesday August 09, 2016 @07:26PM (#12258385)
> I give you, that "%" is bad for named arguments, but why wouldn't you just improve that then?

Perhaps they should. I'm not making a case specifically for the proposed strategy (interpolation), just that if they are going to implement it, that it took this long is odd, and somewhat laughable. Personally, I think the named string format is superior to this new feature in many respects except the most simplistic of cases, but I don't often write python, so my opinion may be colored by that.

Re:PyPy gets funding from Mozilla for Python 3.5 support by dagw on Wednesday August 10, 2016 @03:44AM (#12260112)
List comprehension are interesting and really come into their own when seen as part of the whole family of comprehensions. In addition to list comprehensions, you also have set, dict and generator comprehensions that all share the same syntax.
Re:PyPy gets funding from Mozilla for Python 3.5 support by icebraining on Tuesday August 09, 2016 @06:41PM (#12258211)
Python also has map(), but the lack of lightweight block syntax (like Perl's and Ruby's) makes it more cumbersome to use. I don't see how list comprehensions are less powerful, though.
Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Tuesday August 09, 2016 @07:57PM (#12258539)
> Python also has map(), but the lack of lightweight block syntax (like Perl's and Ruby's) makes it more cumbersome to use.

Yes. Additionally, I prefer the clear directionality of control and transform denoted by the dual capabilities of `map` and `grep` over list comprehensions, especially when chaining. Even better, IMO, is Perl 6's ability to use the feed operator to go left-to-right, so if the map block can be read the same direction as the flow of list items.[1] Having to scan back and forth to determine what is happening is a real detriment to the readability of these types of expressions, IMO.

> I don't see how list comprehensions are less powerful, though.

You're correct. Not strictly less powerful, just that the single expression for transforming the item may require multiple comprehensions where a single slightly more complex map block could get away with a temporary variable. Given that I dislike the way multiple list comprehensions read, this may be a bigger deal for me than most.

1: E.g. my @new = ( @original ==> grep { ... } ==> map { ... } ==> sort { ... } );

Re:PyPy gets funding from Mozilla for Python 3.5 support by zardeh on Tuesday August 09, 2016 @08:32PM (#12258691)
just checking to be sure, your example is essentially

original.filter(...).map(...).sort(), which in python would be `sorted([... for x in original if ...], ...)`.

I'll agree that sometimes the functional/chainable approach is nicer, but often it also isn't necessary

Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Tuesday August 09, 2016 @09:30PM (#12258907)
> just checking to be sure, your example is essentially original.filter(...).map(...).sort()

Yes, a method chaining format (such as Ruby and JS, and which can be enabled with a module in Perl) does approximate the control flow I'm referring to, The only different to the feed operator in Perl 6 is that the feed works on a per-item basis, allowing the list to be lazily generated, which also means it's a closer approximation to how you would code it in a for loop, as statements are done in succession on each item.

E.g.

    my @source = 1..Inf;
    my @elevensies = ( @source ==> map { $_*11 } );
    my @odd-elevensies = ( @elevensies ==> grep *%2 );
    my @root-of-odd-elevensies = ( @odd-elevensies ==> map *.sqrt );
Although in this case the left feed operator may be clearer.

> which in python would be `sorted([... for x in original if ...], ...)`.

> I'll agree that sometimes the functional/chainable approach is nicer, but often it also isn't necessary

Well, the example I gave was simplistic and to show direction. A more complex example would require a comprehension of a comprehension, which is where I start to have real issues with the format, and which is not represented from that example, which wasn't meant to illustrate that particular problem.

Re:PyPy gets funding from Mozilla for Python 3.5 support by icebraining on Wednesday August 10, 2016 @02:06AM (#12259794)
Your example translated to Python is also lazily generated (it uses the lazy equivalent of list comprehensions, called generator expressions):

  source = count(1)
  elevensies = (x * 11 for x in source)
  odd_elevensies = (x for x in elevensies if x%2)
  root_of_odd_elevensies = (math.sqrt(x) for x in odd_elevensies)
To me, the examples seem fairly equivalent, with the exception that Python's is, in my opinion, more readable for a newbie, since it doesn't require knowing map() or grep() - just for loops and ifs.
Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Wednesday August 10, 2016 @03:36AM (#12260080)
I was aware of generators in python, but I wasn't aware of the lazy lest comprehensions, which is very nice.

I agree list comprehensions in the simple form are easier for a newbie to recognize, but I also like the way map and filter make you think about your actions, and think they are clearer in the less trivial cases (as I mentioned above). I say "filter" because while I am completely familiar with that terminology, I accept that it's an unneeded departure from the common terminology for that task.

Re:PyPy gets funding from Mozilla for Python 3.5 support by icebraining on Wednesday August 10, 2016 @04:16AM (#12260231)
Well, map and filter may be more clear in certain cases, but I still don't see how they are more powerful. Seems like you can do a 1-for-1 translation between the two.
Re:PyPy gets funding from Mozilla for Python 3.5 support by kbenson on Wednesday August 10, 2016 @10:19AM (#12261916)
Oh, that's because you must have missed a few comments back where I agreed with you and backed out on any claim they were strictly more powerful. ;)
Re:PyPy gets funding from Mozilla for Python 3.5 support by icebraining on Wednesday August 10, 2016 @10:48AM (#12262156)
Ah, uh, sorry ;)