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 005 - Magic Methods and Winding Up OOP

Done with Reuven Lerner’s OOP basics Notes len does the right thing across multiple types of objects. it counts characters in a string, elements in a list, and key-value pairs in a dictionary. how does it know how to do that? that is because len uses a magic method (methods that have a __ for a prefix and suffix) len uses the __len__ method which i can use in my own classes, to implement a len method for my classes If I choose to do so, I need to realise what those underlying methods expect to return and return the same type in my classes. __len__ expects to return integers, so when I implement it in my classes, I ought not to return a string. Once again Reuven strongly suggests using common sense :) same thing when i try to print something. it calls the magic method __str__ to do stuff. or if i try to do repr to check for raw representations, the __repr__ method is called. Python is filled with these methods which I can use to allow my classes to have standard Python like functionality. like a print(doggie) should give me details about the cute pup object i just created, if I have taken the time and attention to implement the __str__ or __repr__ methods most of these methods come with the base primitive class object which every class inherits from. so i get them for free. and then i can override them and customise them to what I want to show. people try standard methods on new objects. implementing them is much better than creating my own and asking people to use ’em. if both are equal or really similar, ok to just use only __repr__. later when I have experience and other needs, i can implement __str__ too learning / feedback/ experiences from doing exercises Despite warning myself above, I went ahead and made the mistake of returning a whole lovely string, instead of an integer, when I tried to implement the len method on my class XD and then I forgot to add the f prefix to an f-string and wondered why it was not interpolating for nearly 30 mins Reuven has a wry sense of humour. In one of my exercises I get to create classes of animals for a zoo, with various attributes such as colour and legs and so forth. Then he has me put them in cages. After making me put 2 sheep in a cage with a wolf, he wonders if the legs of the sheep ought to be reduced or not XD The number of typos I am making is staggering. Missing colons, missing quotes, missing brackets. If this is Python, I shudder to think how I’d fare if I picked up a static language to learn XD Wasted another half an hour because I spelt colours with capitals like Black and then later looked for black and was tearing my hair out, because I knew there were black animals and why in tarnation, would Python not show me them XD Saw Reuven do nested comprehensions and now I want to do that too! it took him one well thought out line to do what I did in 2 multiline functions Final Words This was amazing! I learnt so much about classes and the way they work. Reuven has a very joyful pedagogy. I love him go hahahaha like Santa Claus. I love the fact that he makes mistakes. I love that for every problem, he poses, he has various approaches. His fluency shines through every lesson. I got lucky with the Pycharm Humble Bundle. But I loved this so much, that I am going to sign up for all the Lerner courses, I possibly can. Beginning with the basics. If OOP is any indicator, the journey is going to be lots of fun. ...

July 12, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 004 - Class Attributes and Inheritance

Learnt about Class Attributes and Inheritance, today. Notes The way we write functions/methods and define classes might look similar, class CrazyTalk(object): and def how_now_brown_cow(): The way they behave/execute, though is wildly different. Classes run / spring to life as soon as the program launches. Functions don’t, unless they are called. Realised that objects are containers. What you can do with the object depends on what is in it. Does it hold a dictionary with various methods to modify it? Does it just hold a bunch of text? Reminds me of media formats as containers. A mp4 file is not just an mp4 file. The video resolution could vary from one file to another. The type of audio encoding could be different. It could carry multiple streams of audio or video or other metadata. What you can do with the file or where you can play it, depends on what is inside it. Same with classes and objects. ...

July 11, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 003 - Methods

Learnt about methods today. Notes follow … My understanding about methods? They are functions in classes that help me manipulate the data the objects contain when they are created. I have been using something them subconsciously all along. The __init__ method, that is called/run automatically every time an object is created. Aha A nice way to unpack the .notation i use Let’s say I have a class SquareNumbers that has a function (method) ­— x2 — that will, you know, square a number. And then I create an object f with a number and call the square method on it. ...

July 10, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 002 - Basic Exercises

Did a few exercises today. They were simple. Create a few classes, change them, modify them, use a list as an attribute and so on. In a couple of ways, this was just what I needed. One, I had an extremely busy day at work, and so I did not have the brain power to do anything complex, so I needed the bar really low any way. And two, I’ve realised, that I have always tried to just read a book and then leap mountains. I don’t know why. I must be a sucker for pain. And then when stuff does not work, I sulk and get frustrated. Easing into hard topics like this, makes it more enjoyable for me and I learn better. This is a meta skill, I should remember to use. Come to think of it, everything I have learnt recently, actually learnt, has been this way. I learnt Dvorak and touch typing, slowly, key by single key. I learnt to diet and lose weight, by tens of grammes in the beginning. ...

July 9, 2020 · Mario Jason Braganza