Posts

Showing posts from June, 2021

If __name__=='__main__'

# u can import this whole by import its file name def printhar (string): return f"ye string harish ko de de { string } " def add (num1 , num2): return num1 + num2 + 5 print ( "u came from" , __name__) if __name__ == '__main__' : # this will not execute below given syntax print (printhar( "takur" )) print (add( 5 , 5 ))

Generators

''' iterable -__iter__() or __getitem__() iterator - __next__ iteration - generator is a type of iterator we can iterate generator only one time ''' def gen (n): for i in range (n): yield i g = (gen( 3 )) print (g) print (g. __next__ ()) print (g. __next__ ()) print (g. __next__ ()) for i in g: print (i) h = "harish" # string is iterable for i in h: print (i) h = "harish" ier = iter (h) print (ier. __next__ ()) print (ier. __next__ ()) # this will give error because int is not iterable '''h=663837 ier=iter(h) print(ier.__next__()) print(ier.__next__())'''

Function caching

from functools import lru_cache @lru_cache ( maxsize = 1 ) def func1 (n): import time #some time consuming task time.sleep(n) return "harish" if __name__== '__main__' : print(func1( 3 )) input( "enter any key:" ) print(func1( 3 ))

Else & finally in try except

  f2 = open ( "harish.txt" ) try : f = open ( "does.txt" ) # different types of except except Exception as e: print (e) except IOError as e: print ( "IO error ha gaya hai" , e) except EOFError as e: print ( "EOF error ha gaya hai" , e) # this will run only if except is not running else : print ( "this will run only if except is not running" ) finally : # this will execute any way print ( "run anyway" ) f2.close() print ( "important stuff" )

Coroutines

def searcher (): print ( "entered into searcher()" ) import time #some time consuming task book= "this book is about a famous software developer mr.N.Harish" time.sleep( 3 ) while True : text=( yield ) #this means i want use this function 'searcher' as coroutine. now it is not a function it is coroutine print ( "started using function 'searcher' as coroutine " ) if text in book: print ( "text is in the book" ) else : print ( "text is not in the book" ) search=searcher() #instance of searcher coroutine print ( "search started" ) if __name__== '__main__' : next (search) #start coroutine from text=(yield) print ( "next method run" ) search.send( "software harish" ) #send values to coroutine input ( "enter any key:" ) search.send( "Harish" ) print ( type (search)) ...

Static methods

class Employee: no_of_leaves = 8 def __init__ ( self , aname , asalary , arole): self .name = aname self .salary = asalary self .role = arole @classmethod def from_str ( cls , string): '''params=string.split("-") return cls(params[0],params[1],params[2])''' return cls (*string.split( "-" )) @staticmethod def why (string): print ( "what is u r name" + string) karan = Employee.from_str( "karan-25000-datascientist" ) print (karan.salary) karan.why( " boy" ) Employee.why( "boy" ) # u can access from class also

Abstraction & encapsulation

  #Abstraction is used to hide the internal functionality of the function from the users. The users only interact with the basic implementation of the function, but inner working is hidden. User is familiar with that "what function does" but they don't know "how it does." #Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data.