A Hundred Days of Code, Day 017 - Python, Advanced Objects, Done!

And we are done with objects! This course finally gave me what I was looking for all these months. The ability to think of and reason about Python, so that I can then think of and reason about, how to build my own programs. Notes Abstract base classes Sounds all high and mighty, but does something pretty logical and simple Helps me test if my object is of a generic (of sorts) type. I don’t want to test if 5 is an integer or a float or any of the other various county types in Python. I just want to see if it’s a number, of some sort. The abstract base class numbers helps me do that. There are such classes for strings and files, I hear. Will go into learning more about them in detail later, as I actually write more Python. But at least I know what they are now. ** Part 3, Context Managers** ...

July 25, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 016 - Python, Advanced Objects.

Started up with Python Objects today. This seems like a long, hard one. Here’s hoping I learn lots. Knowing Reuven, I know I will. Notes Part 1, Advanced Methods. This section focuses on the dunder methods in Python. By default objects are not equal to other objects even they they are from the same class, with the same attributes and methods. We could implement our own equals method though. The point being, we need to be intentional and methodical, when we design and implement our classses Static methods are plain old functions that I write in my class for use by both the class and the instance. Could be used to clean up input or similar such utility functions. I create them by putting a @staticmethod decorator atop my method. ...

July 24, 2020 · Mario Jason Braganza

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