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.
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]).
No comments:
Post a Comment