r/Python • u/fishburne • Sep 09 '15
Pep 498 approved. :(
https://www.python.org/dev/peps/pep-0498/13
u/oconnor663 Sep 09 '15
Just this week I was writing some CoffeeScript and thinking how nice it would be if Python had those inline string expressions. I had no idea this was a PEP.
This is going to make a huge difference for programs with lots of multi-line strings. One of my projects has a test suite full of YAML, and inline formatting would've made those strings much easier to read.
The discussion of people doing scary things with locals() reminds me of why Guido chose to add the ternary operator. People were already doing a and b or c, which wasn't safe or readable.
2
u/stevenjd Sep 09 '15
scary things with locals()
What scary things with
locals()? The semantics oflocals()is simple and straightforward. The only "scary" thing about it is that it's slow in PyPy, but that's considered a performance bug.As far as
a and b or c, that was the recommended way to do it for about 15 years, until Guido himself got bitten by the bug, ifbis falsey it gives the wrong result. I don't see what connection that has to "scary things with locals()".2
u/oconnor663 Sep 09 '15
Here's one of the things the PEP pointed out:
In the discussions on python-dev, a number of solutions where presented that used locals() and globals() or their equivalents. All of these have various problems. Among these are referencing variables that are not otherwise used in a closure. Consider:
>>> def outer(x): ... def inner(): ... return 'x={x}'.format_map(locals()) ... return inner ... >>> outer(42)() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in inner KeyError: 'x'This returns an error because the compiler has not added a reference to x inside the closure.
1
u/fishburne Sep 09 '15 edited Sep 09 '15
How about this?
def outer(x): def inner(x=x): return 'x={x}'.format_map(locals()) return inner print(outer(10)())2
u/oconnor663 Sep 09 '15
Yeah, the PEP lists a very similar fix. But the point is that these errors can creep in. Maybe your closure gets invoked far away in the program. And so a few months later someone refactors this code, but forgets to leave in the "unnecessary" variable reference. Now an innocent change has caused a hard-to-debug crash somewhere random. Even worse, it could cause a crash in a codepath that you don't normally test, so that you don't even know which change was at fault.
This is edge case stuff, to be fair, but it's bad enough to disqualify this approach as the Official Recommendation I think.
1
u/fishburne Sep 10 '15
But the point is that these errors can creep in.
That can happen with this pep also. What is stopping someone from removing the variable in the external scope because it is hard to find its reference in the closure. With the default argument method, at least the importing of variable from external scope is explicit and nearer to the use.
→ More replies (1)1
u/stevenjd Sep 10 '15
Well duh. You're using locals(), so you get ... hang on, I better look it up in the docs... locals. Whew. Good thing I checked the docs, that could have been confusing.
-3
u/fishburne Sep 09 '15
and inline formatting would've made those strings much easier to read.
You could do this right now with format() and locals() function, right? And what are the scary things people doing with locals() and how is it relevant?
38
u/mackstann Sep 09 '15
And I thought having two primary string formatting methods was already hypocritical. Now we have three.
(I'm not counting string.Template, as it seems little used to me)
Can Python retain its ethos of simplicity and obviousness when it seems like the only major improvements made to it are in the form of additional complexity?
9
u/elguf Sep 09 '15
Although, having more options for string formatting makes the language more complex, string interpolation is superior to the existing alternatives.
Should the language not evolve/improve in order remain simple?
→ More replies (13)1
u/mackstann Sep 09 '15
The endless additions to the language are not necessarily the problem -- the problem is that they're also not removing things. So it just grows forever.
If they'd remove one or two of the old string formatting methods, I wouldn't be so bothered.
1
u/elguf Sep 09 '15
Removing any of the existing string formatting methods would break backwards compatibility. Surely, that is a worse state of affairs.
2
u/mackstann Sep 09 '15
They've had many years to get it over with, including the Python 3 transition. I don't think that gradual deprecation and removal of certain features is such a bad thing. It's certainly better than never-ending bloat, especially for a language that touts simplicity as a defining characteristic.
1
u/elguf Sep 09 '15
I agree that gradual deprecation and removal is good. However, the first step in that process is adding a better alternative. PEP 498 is the better alternative.
I think PEP 8 will be updated to include recommendations on which string formatting method is preferred. When the next backwards incompatible release comes (if ever), the redundant obsolete features might get removed.
1
u/freework Sep 09 '15
Why can't you just remove all the other string formatting methods from your own personal toolbox. Why does it have to be removed from the language entirely?
1
u/mackstann Sep 09 '15
Because the language espouses simplicity and obviousness, "one way to do it" as its explicit philosophy.
I'm just calling it out. The problem isn't me -- the problem is that the language is growing to the point where it is contradicting its own goals.
17
u/Funnnny Sep 09 '15
it's almost the same as format, I've seen too many locals() to hate this PEP.
→ More replies (10)1
u/pork_spare_ribs Sep 09 '15
string.Template is mostly for building string parsing into your app (eg the user supplies format strings), so the use-case is a little different to the others.
110
u/desmoulinmichel Sep 09 '15 edited Sep 09 '15
To thoose saying it's not explicit, using the "f" to signal you are going to use it is the whole point. It's not more magic than decorators are. Complaining it's magic it like saying that doing:
def func():
pass
func = decorator(func)was a perfectly good way of doing it and that:
@decorator
def func():
passIs too much magic.
f-strings are syntaxic sugar for format(), really. There are NOT string litterals. There is nothing you can do with it you couldn't do with format() (including not escaping user input), so security concerns are off.
Readability concerns are off as well, as it's just the same string-like notation as the one you use for format(), without the format, and prefixed with and "f", easy to spot, and as easy to read as before.
The only annoying point is that it's a dent in "there is only one way to do things" and this syntaxe accepts ANY kind of Python expression (again to gain parity format()), which is an open door to many readability abuses. But so does lambda and list comprehensions, and we learned to behave. I guess editors and linter will need some serious updates.
Other than that, it's quick to write, simple to read, and explicit.
I love this PEP. New comers will love it too : manual string substitution is a pain to teach right now.
→ More replies (13)5
u/metaphorm Sep 09 '15
I make fairly heavy use of decorators in some of my Python code, in particular, when I'm following a more functional style of coding. I would still criticize the decorator syntactic sugar as being much more magical and much less explicit than is typical for Python.
I find them useful in some contexts, so I use them, but I am always intensely aware of the downsides. The biggest downside in my experience is related to training new developers. I've recently been onboarding a new junior developer at my dayjob and the single most difficult part of the codebase for him to understand has been a portion I wrote that made heavy use of decorators. For that reason I sincerely question whether or not new comers will love it too.
Elegant, magical-looking syntactic sugar is a convenience for experienced developers who understand what its doing under the hood. I don't think it helps new comers at all. Its counter-productive for them, in my experience.
5
u/desmoulinmichel Sep 09 '15
Does the newcomer in question understand bla = bla(func) ? If yes, then it's just syntaxic sugar for this. If not, the problem doesn't lie in the syntaxic sugar part but in the concept of the decorator pattern itself, which is another debate entirely and has nothing to do with magic since it's purely a design pattern debate.
1
u/metaphorm Sep 09 '15
Does the newcomer in question understand bla = bla(func) ?
yes. that pattern is relatively common in Javascript (except usually done with an anonymous function, which Python doesn't properly have), so he had seen it before. ultimately that is what helped him understand the decorator sugar.
don't get me wrong, we still use decorators. they are very elegant and once you understand how they work it makes your code much more readable. its a learning curve issue, though.
1
u/zardeh Sep 09 '15
Nah decorator syntax is different. Its specifically
f' = bla(f)where blah is
def blah(f: FunctionType) -> FunctionType: ...you aren't just passing a function to a function, that's a really common pattern (its polymorphism). Decorator is specifically that you create a new function (callable), which is a bit more confusing, and not a common idiom in javascript.
10
u/lamecode Sep 09 '15
I prefer this to the other two methods, but sort of agree that having three is a bit crazy. I don't know the history behind this, but to me it reads like something that should have probably been looked at with the switch to Python 3 - add this new method, drop the %s method.
2
u/takluyver IPython, Py3, etc Sep 09 '15
That kind of did happen - this is based on the .format() syntax, which was introduced for Python 3, and the %s syntax was deprecated. But then they realised that there's loads and loads of code out there using %s, so it got un-deprecated again.
2
u/lamecode Sep 10 '15
True, but there's loads and loads of code out there using the 'print x' method, as well as the old urllib etc. so re-writes were needed either way. Without depreciation, no one is going to get off their butts and migrate to the new methods.
49
u/dysan21 Angry coder Sep 09 '15 edited Jun 30 '23
Content removed in response to reddit API policies
33
u/adrian17 Sep 09 '15 edited Sep 09 '15
Agreed. I understand the "explicit vs implicit" and anti-zen arguments and I can't disagree, but at the same time out of all these:
print('stuff %s thing %s stuff' % (num, name)) print('stuff %(num)s thing %(name)s stuff' % { 'num': num, 'name': name }) print('stuff %(num)s thing %(name)s stuff' % locals()) print('stuff {} thing {} stuff'.format(num, name)) print('stuff {num} thing {name} stuff'.format( num=num, name=name )) print(f'stuff {num} thing {name} stuff')The last one does seem the most readable (or at least the least distracting) to me and I predict I'll quickly start using it for some simple messages, logging etc without feeling bad about it.
→ More replies (5)1
u/gammadistribution Sep 09 '15
Look, the proper analog is this:
print('stuff {num} thing {name} stuff'.format(**parameters))It doesn't take up any additional room and isn't harder to read, but it does obfuscate what exactly is being formatted, just like with f-strings.
Like, where exactly is num and name coming from?
num = 4 def f_string(): num = 3 print(f'{num}') if __name__ == '__main__': f_string()What should this return?
7
u/zigzagEdge Sep 09 '15
It returns the same thing as this:
num = 4 def f_string(): num = 3 print('{num}'.format(num=num)) if __name__ == '__main__': f_string()1
u/Axxhelairon Sep 09 '15
did you fail out of your first computer science class?
1
u/gammadistribution Sep 10 '15
Yeah, totally did. Good conversation.
1
u/Axxhelairon Sep 10 '15
It's barely even sarcasm, you don't know what scoping is ...? You think what you wrote is in any way ambiguous?
→ More replies (2)27
Sep 09 '15
Yeah to be honest this seems super intuitive to me. I think most people who didn't even know about it could read it and immediately understand it. Anyone claiming that this is "less readable" than % or .format need to explain their rationale.
11
u/c3534l Sep 09 '15
The most popular systems for string formatting seemed so archane and bizarre to me. I think this is much more user friendly and it's readable, too, since you can actually see what it is inside the string. And it's essentially just building on the whole {0}, {1} thing. I don't see how something like:
"I got a new {0} today during my trip to {1} for only {3} bucks!".format(purchase, place, amount)Is easier to read than something like:
f"I got a new {purchase} today during my trip to {place} for only {amount} bucks!"Do programmers not read left to right instead of bouncing from the end to the beginning repeatedly?
3
Sep 09 '15
The argument is that you now have static string content and variables mixed up. In the first example, they're clearly separate. Makes it more difficult to locate variable references. Not that I agree at all, syntax highlighting should easily solve that problem, but that seems to be the largest complaint.
→ More replies (8)4
u/PrimitiveDisposition Sep 09 '15
People read from left to right. I don't like the way it looks and think that it does complicate the syntax by adding a new rule, but it makes sense.
"I got a new {0} today ...requires the reader to look to the end of the line.
13
u/kemitche Sep 09 '15
If you don't like it, don't use it.
When code is read more than written, that doesn't apply. Newcomers to Python now will need to learn about 3 different styles of string interpolation. Python's a pretty easy language to learn, but keeping it that way requires diligence.
Adding another string interpolation method that provides minimal improvements over the first two seems very anti-zen of python - "There should be one-- and preferably only one --obvious way to do it."
6
u/stevenjd Sep 09 '15
3 different styles of string interpolation
Four ways. Everyone forgets string.Template :-)
5
2
u/kemitche Sep 09 '15
Well, I've yet to see it used in the wild, so I don't consider it as one of the methods of interpolation that a python programmer would need to learn to read arbitrary code. I see your point though :)
(There's also a million and one 3rd party methods for string interpolation, if you include templating stuff like mako, etc.)
0
u/MrJohz Sep 09 '15
I'd say there's still unambiguously one and only one way to do things, just allowances for different situations. In each of those situations, the one way rule is still held.
If you want to construct a string that contains information about the current context, use f-strings.
If you want to construct a string that can contain information about a future context, use a normal string and the format placeholders.
If you have a context and you want to insert it into an already-written formattable string, use the format method.
There are other ways to do it, but the %-formatting system at this point is largely backwards compatibility, and the Formatter API is for complex formatting situations.
0
Sep 09 '15
[deleted]
→ More replies (3)2
u/kemitche Sep 09 '15
IMO this will be a big win for Python's readability.
I don't disagree that the new method is incrementally better. I simply believe that the minor improvement is not worth introducing yet another method of string interpolation into the core language.
3
u/stevenjd Sep 09 '15
If you don't like it, don't use it.
And don't read/maintain anyone else's code that uses it, right?
-1
u/dysan21 Angry coder Sep 09 '15 edited Jun 30 '23
Content removed in response to reddit API policies
1
5
u/mic_e Sep 09 '15
I have always been annoyed by the verbosity, redundancies and inconsistencies in the current string-formatting methods (passing a tuple to %, the whole "{a}".format(a=a) thing etc.).
In various situations, I ended up using a mix of those methods, whatever produced the most 'readable' and compact code, in my opinion. Both suffer from the left-to-right reading issue, % seems outdated but .format() is just so verbose that it often requires ugly line-breaks to keep the 80-character limit.
The main benefit of f-strings is that syntax highlighting will be easily possible, as there is no ambiguity whether the string is going to interpreted. Once we have syntax highlighting in our major editors and viewers, most of the perceived readability issues will become non-issues.
22
u/kotique Sep 09 '15
We have 3 ways to format string in Python. Why so many? Let'd introduce only one general format to replace all others! Ok, now we have 4 ways to format strings in Python
8
u/flying-sheep Sep 09 '15
We had 2 format string syntaxes (Template is too primitive to count), now we still have two.
The only thing that's new is that there's a new way to use the {} formatting syntax now. And if that's a problem, the logging module using the % formatting syntax is also one.
3
u/tetroxid Sep 09 '15
What happened to "there should only one obvious way to do things"?
1
1
Sep 11 '15
The f-string way is the obvious way to format string.
1
u/tetroxid Sep 11 '15
To you perhaps. To someone used to printf() the % notation is the obvious way. To someone used to Python 3 the .format() method is the obvious way. Perhaps there are people who use string.Template to format their strings. If there were only one way to format strings (whatever it would be) then it would be obvious to anyone to use that form. Adding a fourth option to format strings does not help here.
1
Sep 11 '15
"Although that way may not be obvious at first unless you're Dutch."
I'm not dutch, but I suppose if Guido bless this new way, it is because it's intent is to make it the obvious way
0
u/PlaysForDays Sep 09 '15
Just like reading an article from a writer that's just a little too into synonyms
-6
u/BoTuLoX Sep 09 '15
You'll love Go if you give it a try. The Python world could learn a lot from its simplicity.
2
u/stevenjd Sep 09 '15
Go got strings wrong (8 bit ASCII only). Then they added "runes", their name for text strings, but they got that wrong too.
If Go was invented back in the 1990s, like Java, they'd have an excuse. But now? Too hopeless for words.
3
u/gthank Sep 09 '15
It really boggles the mind that Go managed to mess up strings. Pike was there when Thompson invented UTF-8 for crying out loud.
1
u/BoTuLoX Sep 09 '15
You're misinformed on how Go does strings, you should read this: https://blog.golang.org/strings
Strings are essentially inmutable byte slices, but when you iterate over them with for ... range you iterate over every individual rune regardless of the amount of bytes it has. Go uses UTF-8 internally for everything.
1
u/njharman I use Python 3 Sep 09 '15
python use to be simple.
when go is half the age of python it will no longer be simple.
→ More replies (1)0
6
Sep 09 '15
I welcome this addition. With a language that emphasizes readability so far as to make whitespace semantic, an easy to read/write interpolation mechanism makes sense.
→ More replies (2)
3
u/kwebber321 Sep 09 '15
I'm actually learning python right now as my first language, will this affect my learning?
3
2
u/Pirsqed Sep 09 '15
I don't believe this will affect your learning. It may take a little while for this to make it into a release.
On another note, welcome to Python! If you need any help feel free to pm me or post in /r/learnpython
2
u/kwebber321 Sep 09 '15
thanks for the welcome. Im actually doing some starting courses on code academy.
14
Sep 09 '15
Sweet! Now I can write Perl using Python!
5
u/stillalone Sep 09 '15
Not until we get strings between '/' to to automatically re.compile and add support for =~ operator. And allow variable names to start with '$'
1
u/KagatoLNX Sep 09 '15
Frankly, I've always really wanted something like:
pat = R/some_regex_pattern/g pat =~ dataAs ugly as regexes can be,
re.compileisn't really high-value.1
u/gthank Sep 09 '15
re.compileis a performance optimization. Do some people use it because they think it is some sort of readability enhancer?1
1
u/RalphMacchio Sep 09 '15
Coming from Perl, I do really miss Perl regexes!
1
u/stillalone Sep 09 '15
When I left perl I missed them too but then I got used to just splitting strings around whitespace, with list comprehension and the 'in' operator to identify the necessary components I'm looking for.
I also find re.compile to prebuild regular expressions and use search or match with that compiled expression pretty neat and intuitive so now I don't have too many regular expressions but they're usually pretty clean and easy to follow compared to my Perl code which was one convoluted regex after another.
0
5
u/joanbm Sep 09 '15
Another vote the change is not worth the troubles. Remember, the rationale behind is make string formatting less cumbersome. Not solving some serious issue, not adding missing functionality, but (subjective) view how already existing feature "simplify" - by introducing type (string literals) and related syntax, thus make the core language more complex.
It can point to the wrong design decision in the past, like old vs. new classes, print command vs. function, global functions in builtins vs. corresponding instance methods etc. It happens in Python too often to my taste. Not similar experience with Ruby.
18
u/sadovnychyi Sep 09 '15
2
u/xkcd_transcriber Sep 09 '15
Title: Standards
Title-text: Fortunately, the charging one has been solved now that we've all standardized on mini-USB. Or is it micro-USB? Shit.
Stats: This comic has been referenced 1957 times, representing 2.4598% of referenced xkcds.
xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete
2
u/matchu Sep 09 '15
I'm really not sure how I feel about this. On one hand, three string interpolation syntaxes is too many. On the other, this is by far the most useful of them. At what point do you need to just stop changing the language because it has too many things, no matter how good the new things are? Does that point exist?
1
u/zahlman the heretic Sep 09 '15
It is not a new syntax. It's fundamentally the same syntax as for
.format(), only accessed differently.3
u/matchu Sep 09 '15 edited Sep 09 '15
? That's what syntax means: a mapping from a sequence of characters to an operation. It's a new syntax for the same operation.
But, honestly, saying it's the same as
formatmakes it even weirder. Now, when performing string formatting, you have to choose an operation and then choose a syntax. The ideal number of decisions would be zero, but now it's two :/
2
u/Workaphobia Sep 09 '15
'f' may also be combined with 'u', in either order, although adding 'u' has no effect.
Despite that, I can think of at least one use, for when you really hate the guy who will maintain your code...
2
u/AlSweigart Author of "Automate the Boring Stuff" Sep 10 '15
Yeeeah, got to agree with the frowney face. Syntactic sugar is nice, but too much becomes saccharine. There's a reason "There should be one-- and preferably only one --obvious way to do it." is a Python koan.
4
u/metaphorm Sep 09 '15
I think this is unpythonic in idiom. Don't we usually go with "explicit is better than implicit"? That would suggest that the explicit string interpolation methods that exist (% formating and the format function) are more idiomatic, compared to the f string expressions which has a lot of implicit behavior that requires out-of-band knowledge of the syntax and evaluation results of the f string expression.
2
1
3
u/patrys Saleor Commerce Sep 09 '15
Maybe I am missing something but doesn't it make it impossible to translate the string before interpolation happens?
5
u/alicedu06 Sep 09 '15
Yes it does make it impossible to translate strings. There is a separate PEP for that.
But even if it's not accepted, that's why we have format() in the language : for the times where you need i18n. But let's be real, I write a LOT of Python code, I'm in Europe in a heavy i18n context, and yet I only need translatable string 1 time out of 10. So these are going to be handy.
3
u/mouth_with_a_merc Sep 09 '15
I think this is more a thing for debug print or log messages which you won't translate anyway
0
u/patrys Saleor Commerce Sep 09 '15
Well, you're not supposed to interpolate log messages before you pass them to the logger. It makes it impossible for the logger (and in turn log aggregators) to group similar messages together.
1
u/mouth_with_a_merc Sep 09 '15
Heh, I didn't even know that there's grouping in the logging system...
1
u/patrys Saleor Commerce Sep 09 '15
If you use external log aggregators such as Sentry, they will group together messages that use the same format string (so you end up with the overview telling there were "1234 occurences of 'Sent email to %s' today" rather than a ton of separate messages).
1
u/gdwatson Sep 09 '15
Do you mean translate as in compile or translate as in internationalize?
2
u/patrys Saleor Commerce Sep 09 '15
Translate as in i18n. I do a lot of web development and need to translate more than half of the strings I put in the code.
2
2
u/ModusPwnins Sep 09 '15
print(f'I can ride my bike with no {handlebars}, no {handlebars}, no {handlebars}.')→ More replies (3)
2
u/mipadi Sep 09 '15
So much for "There should be one-- and preferably only one --obvious way to do it."
1
1
u/nicolas-van Sep 09 '15
Not happy either. But let's admit one thing, it's still less horrible than JavaScript's template strings: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
9
1
u/flying-sheep Sep 10 '15
what’s wrong with them in your opinion?
2
u/nicolas-van Sep 11 '15
I mostly disagree with template strings in general for all the reasons that were given in the other comments. I think it's even worse in JavaScript due to the syntax they chose that uses backticks. Backticks are horrible to use in programming languages because they are hard to differentiate visually from apostrophes and are hard to type with a lot of keyboards. In short: they are hard to read and hard to write. That was the worst choice ever.
→ More replies (1)
1
u/Sean1708 Sep 09 '15
So I'm not seeing anybody talk about possible optimisations to this. People always used to complain that format-style strings were slow because they required function calls, could we not treat interpolations similarly to how we treated %-style strings?
0
Sep 09 '15
[deleted]
1
u/Sean1708 Sep 09 '15
When I read this last time I could have sworn it said the strings were translated into the equivalent
format()calls, obviously I was wrong.
0
u/LpSamuelm Sep 09 '15
I don't get the rationale behind !s, !r and !a. Why add this piece of arcane syntax instead of just writing str(expression), for example?
The double brace is also super weird. Why would escaping braces not use backslashes, like, y'know, every other thing that needs to be escaped?
2
u/zahlman the heretic Sep 09 '15
Why add this piece of arcane syntax
It is not being added. This "conversion" step has always been a part of the format string syntax for
.format(). Only theaconversion is being "added", and that's already in 3.5 - which I'd say is just correcting an oversight, given that the builtinasciidoes the same sort of thing thatstrandreprdo.The double brace is also super weird.
It's again the same escaping that
.format()uses.Why would escaping braces not use backslashes, like, y'know, every other thing that needs to be escaped?
Because that isn't even true for the other main method of formatting:
>>> "spam \%s spam %s and spam" % "eggs" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not enough arguments for format string >>> "spam %%s spam %s and spam" % "eggs" 'spam %s spam eggs and spam'See? It even uses the same doubling-up approach. It can't just use a single backslash because that's interpreted (to determine the contents of the format string) at compile-time, while the interpolation is done at run-time. But it doesn't work with a double backslash, either:
>>> "spam \\%s spam %s and spam" % "eggs" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not enough arguments for format stringSure, it works for regexes, but that's more for traditional/historical reasons. (And it's awkward enough that people end up using raw strings for their regexes anyway.)
→ More replies (1)0
u/pdexter Sep 09 '15
Nothing what you are saying right now is new in this PEP.
See PEP-3101
Most people that have issues with this PEP don't seem to know Python. The PEP I linked above is from 2006!
-5
u/hairlesscaveman Sep 09 '15
With the type hint syntax, async* keywords, and now this, it seems Python3 is just becoming an ugly wasteland of "let's pick stuff from other languages and shoehorn them in as quick as we can". It's a shame.
19
u/desmoulinmichel Sep 09 '15 edited Sep 09 '15
Python has always beings stealing from other languages. If you read the mailling list, you'll see that async and f-string have been heavily debated, various forms have been proposed, pros and cons have been brought on the table.
Then the implementation takes time as well, during which the feature is still in evaluation. Some features you never heard of (such as computable dict keys) went trhough this whole process, just to be dropped out.
It's a hell of a work, and it's done very seriously, with passion and commitment. Work you can be part of you if have concerns to raise, since it's all open.
Bottom line, these features are not here by mistake. They are strategic moves toward making Python a modern language. It's a difficult mission, some people will not like it, and it's ok. Right now I feel like Python is finally catching up with the new century, compensating for the last decade of struggling with the 2/3 transition.
Python is alive. It's great. It's cool. And it's going to be fun.
Now let's go back to work.
1
u/beertown Sep 09 '15
Yes! This is the best comment I read so far.
Python is a pot full of great ideas. The folks behind these new f-strings are the same that filled that pot. The odds this is another good idea are very high.
This is a concept stolen from other languages? As long as it is good, who cares. What other languages don't do is steal good ideas from Python.
8
0
-2
-1
u/lightym81 Sep 09 '15 edited Sep 09 '15
I want more limited feature.
feature is new string method.
new method can embed just variable. expression cannot be embedded.
name = "Bob"
age = 25
"{name} is {age} years old.".embed() "Bob is 25 years old."
3
u/mic_e Sep 09 '15
There are two major issues with this:
embed()doesn't know about thenameandagelocals. It'd need to use some horrible magic to use the previous stack frame's locals, and even then, the limitations from https://www.python.org/dev/peps/pep-0498/#no-use-of-globals-or-locals apply.Highlighters can't know whether
embed()will be called, so they're limited.1
u/lightym81 Sep 09 '15
I understood method issues.
but one more point, I think a ugly that is format including expression.
In now PEP, f-string syntax is able to too free anything.
I don't love a below example that might valid.>>> (f"{bob.nickname if bob.nickname else bob.firstname+' '+bob.familyname}'s " ... f"life goes on {(today()-bob.birthday).days} days from birthday.")
74
u/chocolate_elvis Sep 09 '15
Why sad face?