Is it true that I can’t use curly braces in Python?

Python Programming

Question or problem about Python programming:

I was reading that Python does all it’s “code blocks” by indentation, rather than with curly braces. Is that right? So functions, if’s and stuff like that all appear without surrounding their block with curly braces?

How to solve the problem:

Solution 1:

You can try to add support for braces using a future import statement, but it’s not yet supported, so you’ll get a syntax error:

>>> from __future__ import braces
  File "", line 1
SyntaxError: not a chance

Solution 2:

Correct for code blocks. However, you do define dictionaries in Python using curly braces:

a_dict = {
    'key': 'value',
}

Ahhhhhh.

Solution 3:

Yes. Curly braces are not used. Instead, you use the : symbol to introduce new blocks, like so:

if True:
    do_something()
    something_else()
else:
    something()

Solution 4:

Python with Braces is a variant of python that lets you do exactly that.
It’s a project that I’ve been working on lately together with my friend.

Solution 5:

Yup ๐Ÿ™‚

And there’s (usually) a difference between 4 spaces and a tab, so make sure you standardize the usage ..

Solution 6:

Use Whyton:

http://writeonly.wordpress.com/2010/04/01/whython-python-for-people-who-hate-whitespace/

Solution 7:

Yes.

if True:
    #dosomething
else:
    #dosomething else

#continue on with whatever you were doing

Basically, wherever you would’ve had an opening curly brace, use a colon instead. Unindent to close the region. It doesn’t take long for it to feel completely natural.

Solution 8:

Python does not use curly braces for code blocks:

>>> while True {
  File "", line 1
    while True {
               ^
SyntaxError: invalid syntax

>>> from __future__ import braces
  File "", line 1
SyntaxError: not a chance

(Notice the “not a chance” message โ€“ this is an Easter egg reflecting this design decision.)

As a language designed to be easy to use and read, Python uses colons and indentation to designate code blocks. Defining code blocks by indentation is unusual and can come as a surprise to programmers who are used to languages like C++ and C# because these (and many other languages) don’t care about extra whitespace or indentation. This rule is intended to increase readability of Python code, at the cost of some of the programmer’s freedom to use varying amounts of whitespace.

An increase in the indentation level indicates the start of a code block, while a decrease indicates the end of the code block. By convention, each indentation is four spaces wide.

Here’s a simple example which sums all the integers from 0 to 9. Note that ranges in Python include the first value, up to but not including the last value:

j = 0
for i in range(0, 10):
    j += i
print(j)

Solution 9:

>>> from __future__ import braces
  File "", line 1
SyntaxError: not a chance

Well that explains a lot.

Note however, that Python does natively support curly brace-d code blocks! Take a look at below:

if x: #{
    x += 1
#}

For Ada or Pascal programmers, I take delight in revealing to you:

if x: #BEGIN
    ...
#END

Taken from the docs:


Python’s parser is also sophisticated enough to recognize mixed
notations, and it will even catch missing beginning or end
delimiters and correct the program for the user. This allows the
following to be recognized as legal Python:

if x: #BEGIN
     x = x + 1
#}

And this, for Bash users:

if x:
    x=99
#fi

Even better, for programmers familiar with C, C++, etc. you can omit the curly braces completely for only one statement:

if x:
    do_stuff()

Beautiful. As mentioned before, Python can also automatically correct code with incorrect delimiters, so this code is also legal:

if x:
    do_a_hundred_or_more_statements()
    x = x + 1
    print(x)

As this must make you love Python even more, I send you off with one last quote from the docs.


Now as you can see from this series of examples, Python has
advanced the state of the art of parser technology and code
recognition capabilities well beyond that of the legacy languages.
It has done this in a manner which carefully balances good coding
style with the need for older programmers to feel comfortable with
look of the language syntax.

The only limitation is that these special delimiters be preceded by a hashtag symbol.

Solution 10:

Yes you can use this library/package { Py }
Use curly braces instead of indenting, plus much more sugar added to Python’s syntax.

https://pypi.org/project/brackets/

// Use braces in Python!

def fib(n) {
  a, b = 0, 1
  while (a < n) {
    print(a, end=' ')
    a, b = b, a+b
  }
  print()
}

/*
  Powerful anonymous functions
*/

print([def(x) {
  if(x in [0, 1]) {
    return x
  };
  while (x < 100) {
    x = x ** 2
  };
  return x
}(x) for x in range(0, 10)])

Hope this helps!