Python Pass and Empty Class

What is the uses of pass statements ?

  • pass statement does nothing.
  • It can be used when a statement is required syntactically but the program requires no action.

Infinite loop with no action

Just busy wait infinite loop.
#!/usr/bin/python                                                               
                                                                                
while True:                                                                     
    # print("welcome to python pass statement learning!");                       
    pass;   
Output:
$ python pass_test.py

Example python program for pass statement

#!/usr/bin/python                                                               
                                                                                
for num in reversed([1, 2, 3, 4]):                                              
   if num == 3:                                                                 
      pass                                                                      
      print("pass block");                                                      
   else:                                                                        
       print("number :%d" % num);                                               
                                                                                
print "done!"  
Output:
$ python pass_test.py
number :4
pass block
number :2
number :1
done!

Empty Class

Commonly used for creating minimal classes, bundling together a few named data items.
class EmptyClass:
    pass;

How empty class can be used in python program ?

class Student:                                                                  
    pass;                                                                       
                                                                                
# Create an object for Empty Student Class                                      
student1 = Student();                                                           
                                                                                
# Fields of the object student1                                                 
student1.name = 'Student1';                                                     
student1.dept = 'computer science';                                             
student1.mark = 90;                                                             
                                                                                
print("name: %s" % student1.name);                                              
print("department: %s" % student1.dept);                                              
print("mark: %d" % student1.mark); 
Output:
$ python pass_empty_class.py
name: Student1
department: computer science
mark: 90

Not implemented function

pass can be used in any function or conditional body when working on new code. it is allowing to think at a more abstract level.
# Remember to implement this!
def computeSalary(*args):
    pass;   
here pass statement is just ignored. here this computeSalary function needs to be implemented later.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^