0

When I was learning python, I knew that python does not need to use {} like java.

If nothing to do,I need to use pass,for example:

if 1>0:
    pass
else:
    print('no')

But if I don’t want to use pass, I will get an error, I need to use other statements instead, for example:

if 1>0:
    print() #Or assignment statement,like a=1
else:
    print('no')

But writing such a sentence is just for grammatical correctness.

If I don’t want this, I can only use {} instead, for example:

if 1>0:
    {}
else:
    print('no')

OK,I finally saw {} in python, so does the meaning of {} only appear to replace pass?

Except for the case of pass, I can’t figure out where it is needed {}.

  • 4
    ```{}``` doesn't mean ```pass```, you actually created an empty dictionary. – goalie1998 Jan 22 '21 at 06:27
  • If you do `{ print(1) }` of course it would be a set. But if you do `{}` without any value it would be a `dictionary`. – U13-Forward Jan 22 '21 at 06:35
  • `type({print(1)})` gives `set` and `type({})` will give `dict` curly braces can be used for both – Abdul Aziz Barkat Jan 22 '21 at 06:36
  • Sets and dictionaries are all `{}`. It depends if you do `{'a': 1}` then it becomes a dictionary, if you do `{1, 2, 3}` it would be a set. – U13-Forward Jan 22 '21 at 06:37
  • If you store `key: value` pairs inside square brackets(`{}`) it is dictionary, but if you store individual elements its `set`. `print(1)` evaluates to `None`, so essentially it is like `{None}` which is a `set`. – Sayandip Dutta Jan 22 '21 at 06:42

4 Answers4

1

Not only {} works. Other stuff like [] () or just a number like 0 1 would work.

The reason it's used is because it just processes an empty dictionary. And doesn't assign to any other variable:

>>> type({})
<class 'dict'>
>>> 

Doing () or anything else would also work, like 1 or [] and so on...

The reason why people don't use pass more often is because it is bad practice, see here for more.

As we all know we can't do:

if 1>0:
    
else:
    print('no')

That would raise an error. So we would have to take up the space by doing some unneeded things (like {}).

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • "don't use `pass` ... bad practice" I strongly disagree. `pass` is the correct and imao *only* correct way to do nothing where a statement is needed. It is usually a sign that you have a bad pattern that you need to do nothing in that instance, but **if** you need to do nothing `pass` is the canonical way to do it. – Aaron Jan 22 '21 at 07:04
0

{} is an empty dictionary, like {'foo': 1, 'bar': 2} with no elements.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
0

Using {} which means an empty dict is not good practice, if you don't want to use pass, you can use ... which is called ellipsis in python3

if 1>0:
    ...
else:
    print('no')
Menglong Li
  • 2,177
  • 14
  • 19
0

In python you must have a block after the if expression : (see the syntax guide). A block may either be a simple statement (single expression after the colon) or an indented series of one or more statements.

This basically boils down to: "What is a valid statement?" If you follow the chain of what a valid statement can be; even just an atom is a valid statement. This is what you are experiencing as an atom includes most literals like an empty string "", a number, an empty dictionary, the various singletons None, True, False, Ellipsis, etc.

The keyword "pass" was included in the language for the exact purpose of doing nothing in a place where you are required by the syntax to have a block. All other suggestions and options given may work, but were not included in the language for that purpose. It is merely a side effect of how other things happen to work. Most of the examples will cause extra (if small) processing and memory overhead, where "pass" is correctly optimized by the interpreter to do nothing.

You may find people saying "you shouldn't be using pass" or "it's bad to have pass in your code". This comes from the idea that you shouldn't ever be doing nothing because it's wasted computation. For example, if you only need the else block of an if statement, it may be more advisable to change it to if not. It is also a common pattern to find pass in try: except: blocks, and it is generally not advisable to just do nothing when an error is caught.

This does not mean pass doesn't have perfectly legitimate use cases. I particularly use pass all the time when laying out classes and functions I haven't written yet. This allows me to sketch out a class before implementing everything while still being able to run quick tests without syntax errors. When you think you're done with your code it's then trivial to do a quick "ctrl-f" to find any leftover pass statements for things I may have said "I'll get to it later" initially. One could even argue this is a bit lax, as it's a little better to actually raise a NotImplemented error if you hit an unfinished area, and I've been trying to swtich, but thats almost 3x the number of keystrokes ;)

Aaron
  • 10,133
  • 1
  • 24
  • 40