Python with Braces
web.archive.org
web.archive.org
I imagine I could train myself to do so, but there is a cost involved.
This is one of the good things about significant indentation: the human and the computer are reading the same signals.
last week i fixed a bug where a mistake in indentation (maybe a cut and paste in a refactor?) caused something to be done once but not the correct number of times (once for every iteration). took a few minutes to figure out where to look, but it took a few minutes more to figure out what was intended and what the bug ultimately was (the disconnect between what you meant and what you did). while this is a small 2 line example, for larger code blocks this is a serious problem.
i'm not advocating for braces, but they have a helping hand in spotting these sorts of bugs.
that said i often dislike it when people dismiss python just on the whitespace complaint alone.
But I've also committed bugs due to putting something on the wrong side of a closing brace.
> Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings.
If you attempt to do computer's work for them, you're going to screw up. I screw up manual refactorings in Java too (e.g. misspelling variable names, misspelling one of the gazillion xml file declarations), and much more than in Python. Don't do manual refactoring.
The real flaw is that the architecture of python makes automated refactoring impossible. This is not due to the indentation sensitivity of Python.
That said, I do sometimes write in Python and I have nothing against the language. This isn't meant to be an attack on Python.
Edit: also, the indentation in the first example is obviously just a means of illustrating that whitespace is non-significant with this project. I don't think they're suggesting code ought to be written without correct indentation.
Does anyone anywhere have a realistic example of Python's indentation system being a hindrance? To me, the complaint always comes across as "I should hypothetically be able to do things that I would never actually do or endorse doing"
And it's not like there's no flexibility for weird corner cases. I mean, all of these things are already legal Python 3:
for i in range(10): print(i)
# although you wouldn't actually do this
a = 3; b = 5; # a,b = 3,5
# and you REALLY wouldn't do this but I couldn't help coopting their example
if foo == "bar": _=(
print("indenation"),
print("doesn't"),
print("matter!")
)Would be very interested in projects that remove braces from other languages, though!
So, while you can write messy, unindented code, if you run gofmt over it, it'll look like everyone else's code (but probably still messy, because if you can't be bothered to indent your code, why would you be bothered to write clean code?).
Guard statements and low cyclomatic complexity are important in all languages, but doubly so in python.
The culture/community that does exist is more oriented toward helping people achieve what they want to or need to get done, rather than smugly regurgitating alleged "best practices" (Ruby and JavaScript) or absurdity (Perl).
Yes, there are the PEPs, but they're more about providing useful guidelines that can likely offer some benefit if followed, rather than dictating the "best" way of doing something. Avoiding the typical Python approaches may raise some eyebrows or cause some confusion, but basically never the visceral lashing that one would be likely to receive within the Ruby community, for example.
We don't really care about "communities" or "cultures" and whatnot. We just have work that we need to get done using those programming languages. This isn't about "defending the Python culture" or "attacking the Ruby community" or any crap like that.
Having dealt with so many different communities over the years, the Python community is by far the best to deal with, from a practical standpoint. Whenever I've had a Python-related question, the members of that community have done their best to help me out with achieving what I want to do.
This is very different from the typical response from the Ruby or JavaScript communities, where they'll consistently tell you that what you're doing is "wrong" or against their "best practices" of the hour. That's not the kind of "help" that we're looking for.
Get all emotional about this if you want. That's your right. But the rest of us have pragmatic, real-world concerns. The Python community does an excellent delivering us with useful help when we need it. Certain other communities do not. Those are just the facts.
Another commenter suggests the only reason to do something like this would be "emotional attachment"; all the huffing and puffing in these comments strikes me as exactly that, but in reverse.
Surely you didn't mean tabs?
https://code.google.com/p/soc/wiki/PythonStyleGuide : "Indentation: 2 spaces (no tabs), differs from PEP8"
PEP8 (http://www.python.org/dev/peps/pep-0008/#tabs-or-spaces): "Tabs or Spaces? ... Spaces are the preferred indentation method. ... Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively."
Which translates to "yes".
Source: https://code.google.com/p/soc/wiki/PythonStyleGuide
Quote: "Indentation: 2 spaces (no tabs)"
Gee, that's funny -- no mention of "soft tabs".
Source: http://www.python.org/dev/peps/pep-0008/
Quote: "Indentation ... Use 4 spaces per indentation level."
Where are the references to "soft tabs"?
Even then, this seems like a lot of work to overcome a non-problem, akin to developing a C++11 compiler which uses "begin" and "end" instead of braces because of some emotional attachment to Pascal. Really, if you are a programmer and the only thing stopping you from using Python to solve some problem is its indentation rules, you should stop and reevaluate your priorities.
The thing is, it's actually trivial to do that using the C++ preprocessor.
In fact, Steve Bourne wrote the Bourne shell exactly in this fashion, with the following C substitution macros (based on Algol-68):
#define STRING char *
#define IF if(
#define THEN ){
#define ELSE } else {
#define FI ;}
#define WHILE while (
#define DO ({
#define OD ;}
#define INT int
#define BEGIN {
#define END }
Source: Expert C Programming: Deep C Secrets, p. 13. Peter Van der Linden. 1994.(this isn't to deny that Python with Braces is a rather frivolous idea)
And Lisp...well, I shuddered at the thought of learning Lisp because of all those parentheses.
Now, however, I don't give a damn about braces. I use Java if the job calls for it, and I've developed quite an appreciation for C#. I enjoy playing around with Lisp and recognize that it shares a lot of fundamental features with Python. I have a strong appreciation for C's power and control (but use it only under very certain circumstances and with great trepidation).
Python is still my go-to language because of its incredible ecosystem, but I no longer insist on using it for everything. I use the right tool for the job. I dropped Coffeescript in favor of pure Javascript, which I've come to appreciate as not a bad language, as long as you're aware of the traps.
For larger jobs, I usually seriously consider using a statically-typed language, because I know I'll either have to religiously write unit tests, or suffer a world of pain (and even then odd bugs will happen that otherwise would not have).
Point is, I suspect that very often, those people that insist on bringing the syntax from one language to another are either early in their career, or they're focused on the wrong thing. If the presence or absence of braces is the feature of a language that bothers you most, then you want want to reevaluate your priorities.
(that said, if this project was just a fun way to explore Python's internals and lexers/parsers, please carry on!).
I must admit, I don't get why anybody would be interested in using this. Even the headline snippet is an example of "what not to do."
Python's significant whitespace incurs the small overhead of requiring attention to detail when copying and pasting code - that's pretty much the only downside, and it seems strange to add a feature while boils down to "allow slopping indentation practice at the cost of extra syntax."
I thoroughly believe syntactic noise is an enemy of productivity. Good editors will help to some extent, but I find the process of reading (say) CoffeeScript much more pleasant than JavaScript. Each to their own, of course.
Significant whitespace to me is one of the fundamental parts of Python that if messed with, makes it not Python.
Perhaps this will make it slightly easier for people coming from PHP, Javascript or one of the many languages that do use curly braces, but significant whitespace is a selling point, not a hinderance IMO.
map([1,2,3], lambda (x) {
print x
return x * 2
})
That's one of the limiting reason why we can't have that in Python right now >>> from __future__ import braces SyntaxError: not a chanceP.S. I don't program in Python at all. The last time I've programmed in Python was a few years ago, when 2.4 was fresh (and I would still claim I didn't even really programmed then). I think indention base syntax has its advantages and disadvantages. But it's not pure good.
One of the examples on the page says "indentation doesn't matter". But the thing is, it does matter - even if there are braces. Correct indentation means that your code is more readable. Python (the normal version without braces) enforces correct indentation, which is brilliant.
you = {'are': 'screwed'}
[0] https://leanpub.com/underhandedjavascript or its alternate title: https://leanpub.com/jsinterviewquestions [1] Slide 21 (1-index): https://speakerdeck.com/chewxy/underhanded-javascript-how-to...
EDIT:
For those of you who do not want to click through my self promotional links, try this in your browser:
{}[0] == true // false
{}[1] == true // true
!{}[0] == true // true
!{}[1] == true // trueBecause as soon as you see this
{ identifier:
You know it's an object literal and not a block. if foo == bar {
baz(), lorem(), ipsum()
}
could be: if foo == bar:
baz(), lorem(), ipsum()
or: if foo == bar:
{baz(), lorem(), ipsum()}
Admittedly, this code doesn't have a functional difference. With a bit more thought, I may be able to come up with code where that matters.This is doable since dictionaries and blocks show up in different contexts. A block never follows =,:, ;, or (, so with a context sensitive grammar this can be accomplished. I guess it depends how they intend to support anonymous functions (def{}?)
- I'm not the one who behind this project - actually, I'm a not a real programmer (I'm a graphic designer who also code) - the people behind this project can be found in Github. At least I can say about one of the who is a close friend, that he codes in Python about 8 years as a profession - I'll ask him to open a user and add his thoughts to this discussion (if he doesn't have yet)
Fair disclosure: I used a language called Actor that was pretty much Smalltalk without messages. I mean, there were messages. They just looked like function calls with the receiver as first parameter.
+1 : the idea of braces in Python
-1 : The fact that the project addresses an out-of-date Python version (2.7 instead of 3+).
Result: 0
File "<stdin>", line 1
SyntaxError: not a chance