A Hundred Days of Code, Day 015 - Python, Advanced Functions, Done!

Delving deeper into Python functions and learning more about them, using Reuven Lerner’s Advanced Python Functions Course Notes The co_* series of attributes inside the __code__ attribute inside most functions gives a wealth of information about a function. For example, looking up the argcount attribute for some function hello, with hello.__code__.co_argcount will give us the number of arguments hello takes. And doing a hello.__code__.co_varnames will give us a tuple of all the local variables in a function hello.__code__.co_code contains the compiled bytecode for the function. Importing the disassembler import dis and running it on the bytecode, dis.dis(hello) will show us what’s in there. hello.__code__.co_flags keeps a track of whether the function has * arguments or not via a binary switch. (It holds many parameters, one of which is the on/off switch for *args) if said switch is set, the function checks for such arguments at runtime and turns them into a tuple to process. they are set for *args via the hello.__code__co_argcount switch (0/1) and for keyword arguments with the hello.__code__.co_kwonlyargcount switch. the __defaults__ attribute keeps all the functions default variable values. If I don’t supply a value to some parameter, the function will look in here, to see if a default value exists. hello.__defaults__ could hold a last name default, for example, if I want my hello function to work even if no last name was supplied, and it actually expected a full name. do not use mutable defaults. an empty list or dictionary for example. set the default to None first in such case and then create the list when the function runs at runtime. The last thing I learnt about were nested functions. I can nest them two/three/as many levels deep as I wish. Practically, most folks do two or three. When an inner function takes the variables of its enclosure and does something with it and returns stuff…? those kinds of functions are called closures. Learning / Feedback/ Experiences from doing exercises The course so far is Reuven making simple functions and then breaking them down and explaining functionality in painstaking detail. Reuven is an awesome teacher! I would gush about him every post, but then it would get too much 😂. So once per course is good I kept looking at the disassembled code and wondering, what language it was. I still don’t know. My ‘educated’ guess is that it is a level of standard pseudo-assembly code for some phantom standardised processor. Once I had that mental model in mind though, all of it started making a lot more sense. This brought memories of Z80 assembly, I learnt in the mid 90s This was fun to do! Onwards to Advanced Objects! Read all about my Python Functions journey here ...

July 24, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 013, Day 14 - Python, Advanced Data Structures, Done!

Notes and experiences for yesterday and today. I ‘wasted’ a lot of time, again, struggling with exercises yesterday and ended up being too zombied to even write the summary post. Have been much smarter about it today. Stopped after about 30 mins and copied and tried to understand the solution. Notes List of Dictionaries Just more convenient than a list of lists, not particularly faster Make it more flexible. Dictionary of dictionaries ...

July 22, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 012 - Python, Advanced Data Structures continued

Ok! Had a nice refreshing break, yesterday being Sunday. Back to work today! If today’s notes, feel a little light, that’s because I was struggling with exercises. Notes Part 2 A lightweight solution to classes, if I am just combining all sorts of data structures is the built-in collections. Some of them could be List of Lists List of Tuples List of Dictionaries Dictionary of Dictionaries Dictionary of Lists Mostly use them to solve tricky issues. If I find myself using lots of functions, just to deal with these structures, I am better off using classes. These structures also lend themselves to being converted into classes. Sometimes it’s just a matter of adding a few methods, et voilà, I have a class! Lists of lists ...

July 20, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 011 - Python, Advanced Data Structures

Started with a new Reuven Lerner Course, on Advanced Data Structures.. Aiming to comfortably finish this in a week. Notes and experiences, follow. The course is three parts. Part 1 - Deep dive into the common data structures Part 2 - Combining Data Structures. Lists of lists, Dicts of lists, Tuples of Lists and so forth. How far can I go without classes? Or how can I enhance my classes with this approach? Part 3 - Complex data structures, that come with the Python Standard Library. The Collections module, Weak references etc. Notes Part 1 ...

July 18, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 010 - Python Functions, Basics Done!

Today was hard! Notes ‘*args’ way to get multiple arguments without having to assign placeholders for them it scoops up all the arguments and gives it to the function in a tuple. I could then loop over it with for. I just put * in front of one of my placholders et voilà, def example_func(*catch_everything) I can define placeholders to catch specific arguments, but those need to be before my ‘*args’. Edge Case: I could have my arguments in a list/tuple already. I can just add the structure with a * in front of it and Python will unpack it for me. If I have a list, catch_me_if_you_can=[1,2,4] then I can pass the elements individually, as arguments with a def example_func(*[catch_me_if_you_can]) Another kinda frequent edge case: ‘*args’ catches everything that is unassigned. so if i have something with a default value, it will get trampled by the data from my args. Best thing to do is to set the default after the ‘*args’ like so, example_func(a, *args, b=10) Keyword Arguments ...

July 17, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 009 - Python Functions, Basics

Started up with learning about Python function basics, today. Notes follow. Using the Lerner pool of wisdom, as usual. Why do we need functions? DRY (Don’t Repeat Yourself), because we can take stuff that is repeatable, assign it a nickname and just call that nickname over and over, instead of tediously writing all that stuff over and over and over If want to modify something, I can just call it (by the nickname) and then add my little spice to it. Like I have the recipe to an omlette written down somewhere, I can then just add a little note with secret ingredients at the bottom of that recipe to create a better or a different omlette. Functions are objects. ...

July 16, 2020 · Mario Jason Braganza