Python Forum
Purpose of the keyword "pass"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Purpose of the keyword "pass"
#1
Hello Pythonians,
What I understood about the keyword "pass", is that it works exactly like the keyword "continue", except it implements the remaining part of the "clause" that it happens to be in.

In this case, let's say we have the following example:

letter_num = 1
for letter in "Howdy!":
    if letter == "w":
        pass
        print("Encounterd w, not processed ")
    print('Letter ', letter_num, ' is ', letter)
    letter_num += 1

The output of this code will be:
Letter  1  is  H
Letter  2  is  o
Encounterd w, not processed 
Letter  3  is  w
Letter  4  is  d
Letter  5  is  y
Letter  6  is  !
So I do not see what "pass" has done!! the fourth line in the output shouldn't appear. In other word, even if we remove "pass" the output remains the same.

Thanks,
Reply
#2
You use pass when you don't want any action to be taken.

In the example, the pass statement is useless because you want an action - printing to the console.

Continue skips the code after its statement and the next loop is on the go.

Pass will "ignore" the condition and the rest of the code block will be executed. Because after the if/elif/else must be a code block. If you don't want to do anything if the condition is true, pass is doing that.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
pass is a "do nothing" statement. It does nothing. It can be used to provide an empty body for a class, a function or a section of code. For example
class Spam:
    pass
Here is the <doc>!
Reply
#4
(Feb-24-2018, 07:33 AM)Tim Wrote: the fourth line in the output shouldn't appear. In other word, even if we remove "pass" the output remains the same.

I think you mix pass with continue

for letter_num, letter in enumerate("Howdy!", start=1):
    if letter == "w":
        continue
    print('Letter ', letter_num, ' is ', letter)
Output:
Letter 1 is H Letter 2 is o Letter 4 is d Letter 5 is y Letter 6 is !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a specific keyword after another keyword and change the output sgtmcc 5 747 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  How to pass encrypted pass to pyodbc script tester_V 0 807 Jul-27-2023, 12:40 AM
Last Post: tester_V
  What is the useful purpose of Python? dorlow 2 1,674 Sep-16-2020, 04:22 PM
Last Post: JaneOgden91fCL
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,742 Sep-02-2020, 03:32 AM
Last Post: flour_power_33
  What is the purpose of this append command! Captain_farrell 4 2,417 Apr-29-2020, 04:34 AM
Last Post: bowlofred
  Pass by reference vs Pass by value leodavinci1990 1 2,165 Nov-20-2019, 02:05 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020