Home > Software engineering >  Purpose of using pass in example Python function
Purpose of using pass in example Python function

Time:12-16

I'm currently taking a Python course on Udemy and I'm at the point where I'm learning to create my own functions. In the section dealing with functions and tuple unpacking, I was shown the example function:

def employee_check(work_hours):

current_max = 0
employee_of_month = ''

for employee,hours in work_hours:
    if hours > current_max:
      current_max = hours
      employee_of_month = employee
    else:
      pass

#Return
return(employee_of_month,current_max)

My question is simply what is the point of putting pass as an else statement? Messing around, it seems to still operate properly whenever I take it out, is there a use that I'm not aware of or is it just a good habit to get into when creating functions?

EDIT: The purpose of the function being to designate the employee of the month based on total hours worked

CodePudding user response:

Good habit, easier readability, faster to understand. If you read it on a distant future, you know you deliberately wanted to skip doing something on the else condition. Otherwise, you could consider (or someone who is reading your code) that you missed / ignored what would happen in the else condition. A good practice is to think what would future you / other person would think when they see your code. Also, most courses encourage these good practices.

CodePudding user response:

Using pass in your code simply means there is no implementation for the condition. So, it sounds like, I know there is a problem, I am gonna let it pass - rather than not even knowing that there is a problem.

  • Related