Skip to main content

Posts

Showing posts from June, 2022

Inheritance and Built-in function decorators (Class method and Static method)

 https://stackoverflow.com/a/34885285 # multi level inheritance class GrandFather:     def __init__(self, grandfathername):         self.grandfathername = grandfathername class Father(GrandFather):     def __init__(self, fathername, grandfathername):         self.fathername = fathername         super(Father, self).__init__(grandfathername) class Son(Father):     def __init__(self, sonname, fathername, grandfathername):         self.sonname = sonname         super(Son, self).__init__(fathername, grandfathername)          s = Son('SonName', 'FatherName', 'GrandfatherName') print(s.sonname, s.fathername, s.grandfathername) # multiple inheritance class Dad():     def __init__(self, dadname, **kw):         self.dadname = dadname         super(Dad, self).__init__(**kw) #important class Mom():     def __init__(self, momname, **kw):         self.momname = momname         super(Mom, self).__init__(**kw) #important class Baby(Dad, Mom):     def __init__(self, babyname, da

Python deeper

  Everything is an Object https://www.i-programmer.info/programming/python/11683-programmers-python-variables-objects-and-attributes.html https://www.edureka.co/blog/interview-questions/python-interview-questions/ namespaces are variable names. variables and functions are objects in python objects are instance of a class functions are first class objects - meaning they are objects like int, float, bool, list, string etc., they can be passed as arguments, assigned to a variable, returned from a function, etc. - https://www.youtube.com/watch?v=p8RU0JH2xb8 "When one says "everything is an object" (like in Python), does he indeed mean that "everything is first-class"?" : Yes - “First class” means you can operate on them in the usual manner. Most of the times, this just means you can pass these first-class citizens as arguments to functions, or return them from functions. https://stackoverflow.com/questions/245192 Python Variable Scope –Built-in, Global,  Enclo

Deeper into Python value change inside functions

 https://www.dataquest.io/blog/tutorial-functions-modify-lists-dictionaries-python/ number_1 = 5 number_2 = 10 def multiply_and_add ( number_1 , number_2 ) : number_1 = number_1 * 10 number_2 = number_2 * 10 return number_1 + number_2 a_sum = multiply_and_add ( number_1 , number_2 ) print ( a_sum ) print ( number_1 ) print ( number_2 ) 150 5 10   number_1 and number_2 did not change although they were global variables. because python stores global and local variables at different memory locations   def add ( x: float , y: float ) -> float : return x+y this is syntax only. even if you pass strings it will work. initial_list = [ 1 , 2 , 3 ] def duplicate_last ( a_list ) : last_element = a_list [ - 1 ] a_list . append ( last_element ) return a_list new_list = duplicate_last ( a_list = initial_list ) print ( new_list ) print ( initial_list ) [1, 2, 3, 3] [1, 2, 3, 3] global value of initial_list was updated, even thou