Thursday, November 5, 2015

Day 13

Advanced OOP and What the Flask??


The good part about today was that there weren't too many assignments. Unfortunately we also learned a lot in the process without too much practice on what we learned. We started with multiple arguments to pass through functions using the splat operator, which is just putting an asterisk next to the name of the parameter. This means that you don't know how many values will be passed but there will be multiple of the same values. Now splat operators only return tuples. 
  • So def function(*value), and if you call function(4,4), the splat operator will pass the parameters as (4,4). This includes any lists or even other tuples. i.e function((4,4),(4,4)) will return as ((4,4),(4,4)).  
  • A useful trick I learned from a classmate is to write print type(*value) to see what data type is being returned because with the assignment paired with splat operators, a lot of my compiling error was that I kept trying to put tuples with ints or lists with ints. Finding out exactly where the data types are coming from can help you figure out a code to change ints to tuples or vice versa.
After this we went over lambda functions, or the functionalities of lambdas. Lambdas are expressions, not statements so they don't have the same restrictions as statements. Lambdas are primarily used as a shorthand version to write a function with one method but allows for a multi-functional usage of that method.

For example: 
  • def map(self, myList, myLambda): 
  • returnList = []
  • for i in myList:
    • returnList.append(myLambda(i))
  • return returnList
  • print _.map([1,2,3], lambda x: x*x) 
  • This function and this print statement will cause all the values in the list to be squared. But by changing the x*x to other mathematical statements, you can manipulate the list further. We don't change the original function, but we change what we can do with that function in the lambda function. 
  • If it looks confusing, it's not as bad as it looks. myLambda(i) is a representation of the lambda function we write below (the x*x), the myList parameter is just the list we enter after print ([1,2,3]). 
We moved on to Flask really late in the day, much to the chagrin of the cohort. Flask is a framework used to build a single page webpage. I didn't get too much reading done unfortunately since it was so late in the day. Kris went into the lecture and it just kinda flew by my head. Fortunately Flask isn't as hard as I made it out to be although there is a lot of new syntax that I have to learn such as @app.route in my server file and {{session['name']}} in my html.

No comments:

Post a Comment