<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pythonoop on Janusworx</title><link>https://janusworx.com/tags/pythonoop/</link><description>Recent content in Pythonoop on Janusworx</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>feedback@janusworx.com (Mario Jason Braganza)</managingEditor><webMaster>feedback@janusworx.com (Mario Jason Braganza)</webMaster><copyright>© 2026, Mario Jason Braganza</copyright><lastBuildDate>Sat, 25 Jul 2020 18:10:46 +0530</lastBuildDate><atom:link href="https://janusworx.com/tags/pythonoop/index.xml" rel="self" type="application/rss+xml"/><item><title>A Hundred Days of Code, Day 017 - Python, Advanced Objects, Done!</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-017-python-advanced-objects-done/</link><pubDate>Sat, 25 Jul 2020 18:10:46 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-017-python-advanced-objects-done/</guid><description>&lt;p&gt;And we are done with objects!&lt;br&gt;
&lt;a href="https://store.lerner.co.il/advanced-python-objects/" target="_blank" rel="noreferrer"&gt;This course&lt;/a&gt; finally gave me what I was looking for all these months.&lt;br&gt;
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.&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 

&lt;h2 class="relative group"&gt;Notes
 &lt;div id="notes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#notes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Abstract base classes
&lt;ul&gt;
&lt;li&gt;Sounds all high and mighty, but does something pretty logical and simple&lt;/li&gt;
&lt;li&gt;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 &lt;code&gt;numbers&lt;/code&gt; helps me do that.&lt;/li&gt;
&lt;li&gt;There are such classes for strings and files, I hear.&lt;/li&gt;
&lt;li&gt;Will go into learning more about them in detail later, as I actually write more Python. But at least I know &lt;em&gt;what&lt;/em&gt; they are now.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;** Part 3, Context Managers**&lt;/p&gt;
&lt;p&gt;Again, Context Managers, sound like something really complex, but they are just a matter of writing and doing things in a certain manner.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It’s something like a &lt;a href="https://mjbraganza.com/atomic-habits/" target="_blank" rel="noreferrer"&gt;Tiny Habit stack loop&lt;/a&gt;, I learnt in the Atomic Habits book, as I was building up new habits.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;When&lt;/em&gt; I do this,&lt;/li&gt;
&lt;li&gt;&lt;em&gt;then&lt;/em&gt; I will do this immediately &lt;em&gt;after&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;and when I am done, I will do &lt;em&gt;that.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Context Managers have a similar paradigm.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;With this object &lt;em&gt;(as this variable, optionally)&lt;/em&gt;
&lt;ul&gt;
&lt;li&gt;I will run the &lt;code&gt;__enter__&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;&lt;em&gt;then after,&lt;/em&gt; I will do stuff&lt;/li&gt;
&lt;li&gt;and when I am done, I will run the &lt;code&gt;__exit__&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;I can add the context manager protocol to my objects too, by impletementing the &lt;code&gt;__enter__&lt;/code&gt; and &lt;code&gt;__exit__&lt;/code&gt; methods in my classes&lt;/li&gt;
&lt;li&gt;They are used in a particular context, where you want to turn something on and then turn something off, before and after that context, thus &lt;em&gt;Context Manager&lt;/em&gt;.
&lt;ul&gt;
&lt;li&gt;I can set something up in my &lt;code&gt;__enter__&lt;/code&gt; method and then tear it down in my &lt;code&gt;__exit__&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;This could be, for example, a network connection, a file, a redefinition of some sort, etc.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;__enter__&lt;/code&gt; method must return self if all has to work well.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;__exit__&lt;/code&gt; block, takes four arguments, one for &lt;code&gt;self&lt;/code&gt;, one for an error class, one for the object, and one for the address in memory, the traceback. I can use them to catch errors in the main body of my with block, and then decide what the appropriate way to deal with them is.&lt;/li&gt;
&lt;li&gt;A good example is using redirecting the &lt;code&gt;print&lt;/code&gt; function’s output to a file, using a context manager. (The print function can write to a file, when I specify it as an output device like so &lt;code&gt;print (&amp;quot;Hello there&amp;quot;, file=f)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;While I might be able to catch errors in the body of the context manager, I cannot if something happens during &lt;code&gt;__enter__&lt;/code&gt;. just something to be aware of.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Contextlib is a library of context managers, Python already provides us.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;import contextlib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;contextlib.redirect_stdout/err&lt;/code&gt; will redirect stuff from the default output (the screen) to whereever else you want it to.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;** Part 4, Properties and Descriptors **&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;em&gt;&lt;strong&gt;property&lt;/strong&gt;&lt;/em&gt; is an attribute, that acts like getters/setters in other languages. It uses methods to set other attributes in the class (within the bounds we set). For e.g. a &lt;code&gt;.temp&lt;/code&gt; property that will let you set temperatures, but only in the range you specify. not below 18C and not above 25C.&lt;/li&gt;
&lt;li&gt;set by putting &lt;code&gt;@property&lt;/code&gt; atop a getter function (let’s say &lt;code&gt;temp&lt;/code&gt;) and then defining the setter properties in an identically named &lt;code&gt;temp&lt;/code&gt; function again but decorated with &lt;code&gt;@function_name.setter&lt;/code&gt;; in our case &lt;code&gt;@temp.setter&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;the whole actual way to do it is like a contortionist twisting into a pretzel shape. Getting fluent with this will take time and practice.&lt;/li&gt;
&lt;li&gt;it basically (to my mind) is like me giving someone something, and that someone goes and magician like, does something marvellous with that something, instead of just storing it in their pocket. it looks like assigning data to an attribute, but behind the scenes there are methods, just waiting to weave spells on said data.&lt;/li&gt;
&lt;li&gt;When I take a property out of a class and abstact it, so that it can be used by many classes, it’s called a &lt;em&gt;&lt;strong&gt;descriptor.&lt;/strong&gt;&lt;/em&gt; (kinda like abstracting functions out of program files and into modules)&lt;/li&gt;
&lt;li&gt;To kinda formally state, a descriptor is a class whose instances are supposed to be class attributes in other classes.
&lt;ul&gt;
&lt;li&gt;To fit the descriptor protocol, a descriptor class needs to implement…
&lt;ul&gt;
&lt;li&gt;a &lt;code&gt;__get__(self, host_instance, host_class)&lt;/code&gt; method with a corresponding &lt;code&gt;__set__(self, host_instance, new_value_to_set)&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;These are class specific. As in, the descriptor is shared amongst all the instances it is used in.&lt;/li&gt;
&lt;li&gt;To get instance specific descriptors, you set the value that you are changing to be assigned as a dictionary using the host instance as the key and the value, as the value. (More contorting!) and then return the value of the &lt;code&gt;[host_instance]&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;Definitely tons of practice needed with this.&lt;/li&gt;
&lt;li&gt;Not used much, in day to day work, but used heavily behind the scenes by Python itself for its object system. The methods, Python classes have, are plain old functions, with descriptors (in conjunction with weak referenced elements) applied to them.&lt;/li&gt;
&lt;li&gt;The more I dive into all of this, the more Python feels less magical and more like the work of thousands of hard working atomic elements. And I think, I like this better. That everything complex, is actually lots of simple pieces, working together in concert.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;** Part 5, Advanced Topics **&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The only thing I can write here, is that Reuven goes deep into hijacking how Python creates classes and then using them for our own customised ends. How to create my own custom &lt;code&gt;__new__&lt;/code&gt; method, my own custom &lt;code&gt;__init__&lt;/code&gt; method, my own custom &lt;code&gt;type&lt;/code&gt; of object, create my own class with a metaclass…&lt;/li&gt;
&lt;li&gt;Never going to use this, unless I am writing the next Django or something, and even then I doubt, these would be needed 😂&lt;/li&gt;
&lt;li&gt;But this was so much fun to learn!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Read all about my Python Objects journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 016 - Python, Advanced Objects.</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-016-python-advanced-objects/</link><pubDate>Fri, 24 Jul 2020 20:34:43 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-016-python-advanced-objects/</guid><description>&lt;p&gt;Started up with Python Objects today.&lt;br&gt;
This seems like a long, hard one.&lt;br&gt;
Here’s hoping I learn lots.&lt;br&gt;
Knowing Reuven, &lt;a href="https://store.lerner.co.il/advanced-python-objects/" target="_blank" rel="noreferrer"&gt;I know I will.&lt;/a&gt;&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 

&lt;h2 class="relative group"&gt;Notes
 &lt;div id="notes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#notes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Part 1, Advanced Methods.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This section focuses on the dunder methods in Python.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;By default objects are not &lt;code&gt;equal&lt;/code&gt; to other objects even they they are from the same class, with the same attributes and methods.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We could implement our own &lt;code&gt;equals&lt;/code&gt; method though.&lt;/li&gt;
&lt;li&gt;The point being, we need to be &lt;em&gt;intentional&lt;/em&gt; and methodical, when we design and implement our classses&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Static methods are plain old functions that I write in my class for use by &lt;em&gt;both&lt;/em&gt; the class and the instance. Could be used to clean up input or similar such utility functions. I create them by putting a &lt;code&gt;@staticmethod&lt;/code&gt; decorator atop my method.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Class methods are those that are meant for use by only the class. I can call them from my instances, but they act on the class. I create them by using the &lt;code&gt;@classmethod&lt;/code&gt; atop my method. For clarity, the first variable is now written as &lt;code&gt;cls&lt;/code&gt; instead of &lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Part 2, Inheritance.&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MRO, Method Resolution Order. I can do an &lt;code&gt;classname.__mro__&lt;/code&gt; to see how Python goes about looking for methods upwards, specially if I’m inheriting from multiple objects. Running help on an object also lists the MRO of its methods.&lt;/li&gt;
&lt;li&gt;Best to explicity inherit from all parent classes, when inheriting from multiple classes.&lt;/li&gt;
&lt;li&gt;We can use the &lt;code&gt;classname.__bases__&lt;/code&gt; to check for the classes parents.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;mixin&lt;/code&gt; is a class, we can inherit from (probably in addition to and before our parent class), that just adds functionality to our class. Does nothing sematically / organizationally to our objects or classes.
&lt;ul&gt;
&lt;li&gt;The mixin &lt;em&gt;must come first.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Adding an &lt;code&gt;__&lt;/code&gt; before an attribute, signifies that it is not to be touched. A little security by obscurity too. Nothing’s stopping me from actually going and changing things up. It’s just a notifier to people with common sense that such stuff ought not to be tampered with.
&lt;ul&gt;
&lt;li&gt;The main reason though is to prevent conflict between attribute names.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

&lt;h2 class="relative group"&gt;Learning / Feedback/ Experiences from doing exercises
 &lt;div id="learning--feedback-experiences-from-doing-exercises" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#learning--feedback-experiences-from-doing-exercises" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;This course feels like the O’Reilly cookbooks in video form.&lt;/li&gt;
&lt;li&gt;The first section takes all the operators, I take for granted in Python and then shows me how to implement them with my own classes
&lt;ul&gt;
&lt;li&gt;how do I get &lt;code&gt;len&lt;/code&gt; to work on my object? or how do I implement the &lt;code&gt;==&lt;/code&gt; operator in my classes? etc. etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Starting with the last one and this one, have given up on exercises and just taking notes for now. As soon as I finish the run, I will be then going on an exercise binge with these pending exercises as well as Reuven’s &lt;a href="https://www.manning.com/books/python-workout" target="_blank" rel="noreferrer"&gt;Python Workout&lt;/a&gt;.
&lt;ul&gt;
&lt;li&gt;The execption being, if I run into a topic that is &lt;em&gt;really&lt;/em&gt; confusing, then I will attempt the exercises to drill the idea immediately into my head.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Obligatory Reuven plug, since I started up a new course :)
&lt;ul&gt;
&lt;li&gt;Reuven is an awesome teacher.&lt;/li&gt;
&lt;li&gt;He takes the time, to explain stuff in &lt;em&gt;painstaking detail&lt;/em&gt;, leaving nothing as an “exercise to the reader”.&lt;/li&gt;
&lt;li&gt;All of these courses, so far have been worth every single penny I spent on them.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Read all about my Python Objects journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 005 - Magic Methods and Winding Up OOP</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-005-magic-methods-and-winding-up-oop/</link><pubDate>Sun, 12 Jul 2020 19:23:05 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-005-magic-methods-and-winding-up-oop/</guid><description>&lt;p&gt;Done with &lt;a href="https://store.lerner.co.il/courses/object-oriented-python/" target="_blank" rel="noreferrer"&gt;Reuven Lerner’s OOP basics&lt;/a&gt;&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 

&lt;h3 class="relative group"&gt;Notes
 &lt;div id="notes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#notes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;len&lt;/code&gt; does the right thing across multiple types of objects.
&lt;ul&gt;
&lt;li&gt;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?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;that is because &lt;code&gt;len&lt;/code&gt; uses a magic method (methods that have a &lt;code&gt;__&lt;/code&gt; for a prefix and suffix)
&lt;ul&gt;
&lt;li&gt;len uses the &lt;code&gt;__len__&lt;/code&gt; method which i can use in my own classes, to implement a &lt;code&gt;len&lt;/code&gt; method for my classes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;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. &lt;code&gt;__len__&lt;/code&gt; expects to return integers, so when I implement it in my classes, I ought not to return a string. Once again &lt;a href="https://store.lerner.co.il/courses/object-oriented-python/" target="_blank" rel="noreferrer"&gt;Reuven&lt;/a&gt; strongly suggests using common sense :)&lt;/li&gt;
&lt;li&gt;same thing when i try to print something. it calls the magic method &lt;code&gt;__str__&lt;/code&gt; to do stuff. or if i try to do repr to check for raw representations, the &lt;code&gt;__repr__&lt;/code&gt; method is called.&lt;/li&gt;
&lt;li&gt;Python is filled with these methods which I can use to allow my classes to have standard Python like functionality. like a &lt;code&gt;print(doggie)&lt;/code&gt; should give me details about the cute pup object i just created, if I have taken the time and attention to implement the &lt;code&gt;__str__&lt;/code&gt; or &lt;code&gt;__repr__&lt;/code&gt; methods&lt;/li&gt;
&lt;li&gt;most of these methods come with the base primitive class &lt;code&gt;object&lt;/code&gt; 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.&lt;/li&gt;
&lt;li&gt;people try standard methods on new objects. implementing them is much better than creating my own and asking people to use ’em.&lt;/li&gt;
&lt;li&gt;if both are equal or really similar, ok to just use only &lt;code&gt;__repr__&lt;/code&gt;. later when I have experience and other needs, i can implement &lt;code&gt;__str__&lt;/code&gt; too&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 class="relative group"&gt;learning / feedback/ experiences from doing exercises
 &lt;div id="learning--feedback-experiences-from-doing-exercises" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#learning--feedback-experiences-from-doing-exercises" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;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 &lt;code&gt;len&lt;/code&gt; method on my class XD&lt;/li&gt;
&lt;li&gt;and then I forgot to add the f prefix to an f-string and wondered why it was not interpolating for nearly 30 mins&lt;/li&gt;
&lt;li&gt;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&lt;/li&gt;
&lt;li&gt;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&lt;/li&gt;
&lt;li&gt;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 &lt;em&gt;were&lt;/em&gt; black animals and why in tarnation, would Python not show me them XD&lt;/li&gt;
&lt;li&gt;Saw Reuven do nested comprehensions and now I want to do that too! it took him &lt;em&gt;one&lt;/em&gt; well thought out line to do what I did in 2 multiline functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 class="relative group"&gt;Final Words
 &lt;div id="final-words" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#final-words" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;This was amazing!&lt;br&gt;
I learnt so much about classes and the way they work.&lt;br&gt;
Reuven has a very joyful pedagogy.&lt;br&gt;
I love him go hahahaha like Santa Claus.&lt;br&gt;
I love the fact that he makes mistakes.&lt;br&gt;
I love that for every problem, he poses, he has various approaches.&lt;br&gt;
His fluency shines through every lesson.&lt;br&gt;
I got lucky with the &lt;a href="https://www.humblebundle.com/software/python-programming-software" target="_blank" rel="noreferrer"&gt;Pycharm Humble Bundle&lt;/a&gt;.&lt;br&gt;
But I loved this so much, that I am going to sign up for all the &lt;a href="https://store.lerner.co.il/courses/" target="_blank" rel="noreferrer"&gt;Lerner courses&lt;/a&gt;, I possibly can.&lt;br&gt;
Beginning with the basics.&lt;br&gt;
If OOP is any indicator, the journey is going to be lots of fun.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Read all about my OOP journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 004 - Class Attributes and Inheritance</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-004-class-attributes-and-inheritance/</link><pubDate>Sat, 11 Jul 2020 17:47:49 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-004-class-attributes-and-inheritance/</guid><description>&lt;p&gt;Learnt about Class Attributes and Inheritance, today.&lt;/p&gt;
&lt;!-- TEASER_END --&gt;

&lt;h3 class="relative group"&gt;Notes
 &lt;div id="notes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#notes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The way we &lt;em&gt;write&lt;/em&gt; functions/methods and define classes might look similar,&lt;br&gt;
&lt;code&gt;class CrazyTalk(object):&lt;/code&gt; and &lt;code&gt;def how_now_brown_cow():&lt;/code&gt;&lt;br&gt;
The way they behave/execute, though is wildly different.&lt;br&gt;
Classes run / spring to life as soon as the program launches.&lt;br&gt;
Functions don’t, unless they are called.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Realised that objects are containers.&lt;br&gt;
What you can do with the object depends on what is in it.&lt;br&gt;
Does it hold a dictionary with various methods to modify it?&lt;br&gt;
Does it just hold a bunch of text?&lt;br&gt;
Reminds me of media formats as containers.&lt;br&gt;
A mp4 file is not just an mp4 file.&lt;br&gt;
The video resolution could vary from one file to another.&lt;br&gt;
The type of audio encoding could be different.&lt;br&gt;
It could carry multiple streams of audio or video or other metadata.&lt;br&gt;
What you can do with the file or where you can play it, depends on what is &lt;em&gt;inside&lt;/em&gt; it.&lt;br&gt;
Same with classes and objects.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Any thing that we define inside a class, is an attribute. (methods, variables etc.) Reuven made a point of stressing this, so writing this down.&lt;br&gt;
If I just define a variable and try to use it, I’ll get an &lt;code&gt;UnboundLocalError&lt;/code&gt;. I need to use &lt;code&gt;className.variable&lt;/code&gt; when I call it.&lt;br&gt;
So I can just write a variable like so&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeClass&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="n"&gt;crazy_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Muahahahaha&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;		&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SomeClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crazy_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;and then call it with &lt;code&gt;SomeClass.crazy_var&lt;/code&gt; or when I do it &lt;em&gt;after&lt;/em&gt; I define &lt;code&gt;__init__&lt;/code&gt; then I can just directly define it as &lt;code&gt;SomeClass.crazy_var&lt;/code&gt; (which sounds more logical to me), like so&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeClass&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;		&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SomeClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crazy_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;SomeClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crazy_var&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Muahahahaha&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br&gt; 
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A class attribute is associated with the class itself, while object attributes are associated with the objects that are created from the class.&lt;br&gt;
So with &lt;code&gt;car.wheels&lt;/code&gt;, the wheels are associated with the car class, but when I use car to create a Ferrari object, and it has &lt;code&gt;Ferrari.white_wall_rims&lt;/code&gt; the rims are an instance attribute that belong to the Ferrari object / instance and not the car class it was made of.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If I ask for an object’s attribute and it isn’t there, then Python goes looking upwards to check if the class has it. In the previous point, if I go &lt;code&gt;Ferrari.wheels&lt;/code&gt; it’ll work, because Python will check the &lt;code&gt;Ferrari&lt;/code&gt; object and if there are no wheels in there, it’ll go check &lt;code&gt;cars&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Name your attributes carefully.&lt;br&gt;
Don’t have the same names between class attributes and object attributes.&lt;br&gt;
Use your head!&lt;br&gt;
Don’t do that!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When we create a class, that is identical to another with one or two teeny exceptions, when they are so similar that I base the new one on the old one — those new classe are said to be &lt;em&gt;inheriting&lt;/em&gt; from the old one. They have an &lt;code&gt;is-a&lt;/code&gt; relationship.&lt;br&gt;
For e.g. a doggie &lt;code&gt;is-a&lt;/code&gt; floofy animal.&lt;br&gt;
A teddy bear &lt;code&gt;is-a&lt;/code&gt; floofy animal too.&lt;br&gt;
Both of them &lt;em&gt;inherit&lt;/em&gt; their floofiness from the floofy animal class.&lt;br&gt;
I can write that as &lt;code&gt;class Doggie(FloofyAnimal):&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Methods are attributes on a class. They are not attributes of the instance object.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you have an &lt;code&gt;__init__&lt;/code&gt; method in your inherited class, the instance will not look for &lt;code&gt;__init__&lt;/code&gt; variables/details in the parent class.&lt;br&gt;
(it’s like this in Python, Java/C++ can get stuff from the parent / grandparent. this is so, because with Python, everything is a live object whereas with the other languages, the &lt;code&gt;__init__&lt;/code&gt; stuff are declarative rules with orders of precedence (a la CSS) and the class comes alive only after all the rules are parsed.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you &lt;em&gt;do&lt;/em&gt; want to inherit something from the parents &lt;code&gt;__init__&lt;/code&gt;, then you explictly get it by using &lt;code&gt;super()&lt;/code&gt;. as in get it from the supervisor above, i guess? I’d add a line like this to my doggie class&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-Python" data-lang="Python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Doggie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FloofyAnimal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;		&lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;        And then it will pull the name from the Floofy Animal class if it’s in there.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use inheritance only when it makes sense. No need to go hog wild.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 class="relative group"&gt;learning / feedback/ experiences from doing exercises
 &lt;div id="learning--feedback-experiences-from-doing-exercises" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#learning--feedback-experiences-from-doing-exercises" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Slowly getting the hang of list comprehensions. I should practice them more.&lt;br&gt;
Because they are very easy to read later.&lt;br&gt;
Just that, I get very confused while writing them.&lt;/li&gt;
&lt;li&gt;Doing the exercises is slowly getting easier&lt;/li&gt;
&lt;li&gt;&lt;a href="https://store.lerner.co.il/courses/object-oriented-python/" target="_blank" rel="noreferrer"&gt;Reuven&lt;/a&gt; is very hands on, has tons of jokes, and makes tons of typos.&lt;br&gt;
I’ve gone from eyes glazed, looking at the his code and struggling to understand to yelling at the screen when he mistypes something :)&lt;br&gt;
Shows that I am learning :)&lt;/li&gt;
&lt;li&gt;&lt;a href="http://pythontutor.com/visualize.html#mode=display" target="_blank" rel="noreferrer"&gt;Python Tutor&lt;/a&gt; is very handy to see the the thousands of mistakes I make. &lt;br&gt;
Even that interface does not quite make me understand, but it gets me 80% of the way there.&lt;br&gt;
&lt;em&gt;(like when a &lt;code&gt;return&lt;/code&gt; statement of mine was nested in a loop instead of being out at the end of the for statement.)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Read all about my OOP journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 003 - Methods</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-003-methods/</link><pubDate>Fri, 10 Jul 2020 19:44:34 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-003-methods/</guid><description>&lt;p&gt;Learnt about methods today.&lt;br&gt;
Notes follow …&lt;/p&gt;
&lt;p&gt;My understanding about methods?
They are functions in classes that help me manipulate the data the objects contain when they are created.&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 
&lt;p&gt;I have been using something them subconsciously all along.&lt;br&gt;
The &lt;code&gt;__init__&lt;/code&gt; method, that is called/run automatically every time an object is created.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Aha&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
A nice way to unpack the &lt;code&gt;.&lt;/code&gt;notation i use&lt;/p&gt;
&lt;p&gt;Let’s say I have a class &lt;code&gt;SquareNumbers&lt;/code&gt; that has a function (method) ­— &lt;code&gt;x2&lt;/code&gt; — that will, you know, square a number.&lt;br&gt;
And then I create an object &lt;code&gt;f&lt;/code&gt; with a number and call the square method on it.&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SquareNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br/&gt; 
&lt;p&gt;What I am actually doing, when I call &lt;code&gt;f.x2()&lt;/code&gt; can also be written as &lt;code&gt;SquareNumbers.x2(f)&lt;/code&gt;.&lt;br&gt;
I am sending a message &lt;code&gt;x2&lt;/code&gt; to the &lt;code&gt;f&lt;/code&gt; object, and it will respond with however/whatever it is programmed to.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;When I define object variables in a class, I should refer to them with a &lt;code&gt;self.variable&lt;/code&gt; when I define methods.&lt;br&gt;
I figure because each object can have it’s own set of values for those variables and I want Python to find them properly.&lt;/p&gt;
&lt;p&gt;Also learnt this is not the case in most other languages.&lt;br&gt;
With some languages, you set and get values of variables of the object, using even more explicit methods, call getters &lt;em&gt;(accessors)&lt;/em&gt; and setters &lt;em&gt;(mutators).&lt;/em&gt;&lt;br&gt;
These are just fancy names for explicit methods to assign and then read data from the variables of a class.&lt;br&gt;
In our example above, I would probably have a &lt;code&gt;set_x&lt;/code&gt; function defined to set the value of &lt;code&gt;x&lt;/code&gt; like &lt;code&gt;f.set_x(20)&lt;/code&gt; (along with a corresponding &lt;code&gt;get_x&lt;/code&gt; to read it)&lt;/p&gt;
&lt;p&gt;This could also be because other programs have different categories of data (private, public) in classes, while Python does not.&lt;br&gt;
Just differences in language design philosophies, I guess.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I can create an class, that can use objects created by other other classes.&lt;br&gt;
For example, I can create a book class and then have several book objects.&lt;br&gt;
And then create a new bookshelf class, whose objects can take all my books and put them in a list/library of sorts&lt;br&gt;
This kind of relationship is called a &lt;code&gt;has-a&lt;/code&gt; relationship in programming parlance.&lt;br&gt;
(like my bookshelf &lt;code&gt;has-a&lt;/code&gt; book on it)&lt;br&gt;
It’s also called composition. I am composing a new class from other classes.&lt;br&gt;
I can do what I want with the list in the bookshelf.&lt;br&gt;
I could take their prices (if i added them) and then sum them up.&lt;br&gt;
I could take the titles and list them alphabetically, or by author.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Aha&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So what is becoming clearer to me is that classes can take user needs, and express them naturally, while leaving the creation of these calculations and listings and sortings with the programming language primitives behind the scenes.&lt;br&gt;
For my book shelf i could just go &lt;code&gt;shelf.sort.title.alphabetical()&lt;/code&gt;&lt;br&gt;
Or if I have a new need tomorrow, hmm, like finding the total prices of all my books, I could just add a &lt;code&gt;sum&lt;/code&gt; method to my bookshelf and I could go &lt;code&gt;shelf.sum()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It basically makes my code, more human. More easy to understand. More natural.&lt;/p&gt;
&lt;p&gt;Another thing that just occured is that I could swap out the primitives beneath the classes. If I find a faster, better way to sum up prices, I could just use that. While the way I would sum the books would still remain &lt;code&gt;shelf.sum()&lt;/code&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Doing the exercises, I realise that I need to learn a lot more Python vocabulary.&lt;br&gt;
I somehow managed to gather a number of variables into a list.&lt;br&gt;
But when I attempted to do it again, it threw an error.&lt;br&gt;
And that’s when I learnt I could &lt;code&gt;extend&lt;/code&gt; a list.&lt;br&gt;
Lots more practice needed!&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;self&lt;/code&gt; thing bit me hard!&lt;br&gt;
I spent at least a 45 minutes trying to find out why my method could not find a variable, despite me defining it correctly. I should’ve obviously referred to it with &lt;code&gt;self.variable&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;Time to call it a day.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Read all about my OOP journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 002 - Basic Exercises</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-002-basic-exercises/</link><pubDate>Thu, 09 Jul 2020 19:15:05 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-002-basic-exercises/</guid><description>&lt;p&gt;Did a few exercises today.&lt;br&gt;
They were simple.&lt;br&gt;
Create a few classes, change them, modify them, use a list as an attribute and so on.&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 
&lt;p&gt;In a couple of ways, this was just what I needed.&lt;br&gt;
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.&lt;br&gt;
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.&lt;br&gt;
Easing into hard topics like this, makes it more enjoyable for me and I learn better.&lt;br&gt;
This is a meta skill, I should remember to use.&lt;br&gt;
Come to think of it, everything I have learnt recently, &lt;em&gt;actually learnt,&lt;/em&gt; has been this way.&lt;br&gt;
I learnt Dvorak and touch typing, slowly, key by single key.&lt;br&gt;
I learnt to diet and lose weight, by tens of grammes in the beginning.&lt;/p&gt;
&lt;p&gt;So, note to self.&lt;br&gt;
Start small.&lt;br&gt;
Do the work.&lt;br&gt;
Show up daily.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Read all about my OOP journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 001 - Beginning With Classes</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-001-beginning-with-classes/</link><pubDate>Wed, 08 Jul 2020 18:08:05 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-001-beginning-with-classes/</guid><description>&lt;p&gt;Notes I’ve taken from &lt;a href="https://store.lerner.co.il/courses/object-oriented-python/" target="_blank" rel="noreferrer"&gt;the videos I watched, today&lt;/a&gt;.
This is my attempt at Feynman-ing &lt;em&gt;(below)&lt;/em&gt;, what I learnt so far.&lt;/p&gt;
&lt;p&gt;Classes and Object Oriented Programming started to come together for me, when I saw &lt;a href="https://ele.janusworx.com/@jason/104295640623569929" target="_blank" rel="noreferrer"&gt;Kushal using them&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To use my father’s carpentry analogy, I could in theory just hammer nails into wood to join them.&lt;br&gt;
But to make a really strong joint, I could use other methods.&lt;br&gt;
I could screw pieces of wood together, which is markedly better than just nailing them.&lt;br&gt;
I could chisel wood and create a dovetail or mortise joint.&lt;/p&gt;
&lt;p&gt;So it all comes down to understanding what I want to do and figuring out the best technique to get it done.&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 
&lt;p&gt;Like I mentioned in the toot, I could in theory just kludge together all my primitives and create something.&lt;br&gt;
But, if I want to create something beautiful and something that I (and others) can understand, I need to use a different method.&lt;br&gt;
Object Oriented Programming, is a popular one.&lt;/p&gt;

&lt;h3 class="relative group"&gt;Objects and Types (In Python)
 &lt;div id="objects-and-types-in-python" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#objects-and-types-in-python" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;We have objects and types/classes.&lt;br&gt;
We can think of them like we do about cells in our body.&lt;br&gt;
Python for the most part is natively made of objects.&lt;br&gt;
Nearly every primitive I have come across in Python is an object.&lt;br&gt;
Strings are objects and work a certain way / Skin cells work a certain way.&lt;br&gt;
Dictionaries work a certain weay / Optical cells in the eye, work in an very specific manner.&lt;br&gt;
Integers are objects and have their own ways of doing things, as do white blood corpuscles.&lt;/p&gt;
&lt;p&gt;Objects are a special type of &lt;em&gt;something.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A class makes objects.&lt;br&gt;
An int class makes integers. A string class makes strings. And so on and so forth.&lt;/p&gt;
&lt;p&gt;With Python even classes (the constructors) are objects.&lt;br&gt;
So, there must be some kind of God / Creator thing that makes these primitives / originator classes.&lt;br&gt;
And there is.&lt;br&gt;
It’s the &lt;code&gt;type&lt;/code&gt; class.&lt;br&gt;
It’s the base class, the &lt;a href="https://en.wikipedia.org/wiki/Turtles_all_the_way_down" target="_blank" rel="noreferrer"&gt;bottom turtle&lt;/a&gt;, if you will.&lt;br&gt;
The &lt;code&gt;type&lt;/code&gt; class creates all these primitive classes, which we then use to create other objects for our use.&lt;br&gt;
The type of the &lt;code&gt;int&lt;/code&gt; &lt;em&gt;(integer)&lt;/em&gt; class is &lt;code&gt;type&lt;/code&gt; as is the type of the &lt;code&gt;str&lt;/code&gt; &lt;em&gt;(string)&lt;/em&gt; class.&lt;br&gt;
They are all objects of type &lt;code&gt;type&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Which basically, again to use a physical analogy means that &lt;code&gt;type&lt;/code&gt; is the iron forge, which i then use to create factories, which i then use to fashion usable things.&lt;br&gt;
The forge lets me create lug nuts and chassis’ and then I take all these, to create a car factory, which lets me make cars of different types and sizes, with varying amounts of power.&lt;br&gt;
The forge lets me create some mixer, which i can then choose to setup different sorts of mixer factories. Some can make ice cream, some can make porridge.&lt;br&gt;
&lt;code&gt;type&lt;/code&gt; (the forge), creates the int class (the factory), which lets me create all sorts of integer numbers to play around with.&lt;br&gt;
&lt;code&gt;type&lt;/code&gt; (the forge), creates the dictionary class (the factory), which lets me store and sling data about in various ways.&lt;/p&gt;
&lt;p&gt;Note: This might not be true of other programming languages (for e.g. int might be a base primitive.)&lt;/p&gt;

&lt;h3 class="relative group"&gt;Attributes
 &lt;div id="attributes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#attributes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;Every class can have attributes (data, or methods to manipulate data).&lt;br&gt;
Those attribute themselves could be classes, having their own attributes.&lt;/p&gt;
&lt;p&gt;Now each of those takes memory.&lt;br&gt;
Python doesn’t gobble up all our memory however, because some (most?) objects share memory.&lt;/p&gt;
&lt;p&gt;Run &lt;code&gt;dir&lt;/code&gt; on an object to get it’s attributes.&lt;br&gt;
For example, I could do this on the &lt;code&gt;str&lt;/code&gt; class to see what it has.&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;__add__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__class__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__contains__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__delattr__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__dir__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__doc__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__eq__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__format__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__ge__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__getattribute__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__getitem__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__getnewargs__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__gt__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__hash__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__init__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__init_subclass__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__iter__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__le__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__len__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__lt__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__mod__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__mul__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__ne__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__new__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__reduce__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__reduce_ex__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__repr__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__rmod__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__rmul__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__setattr__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__sizeof__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__str__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;__subclasshook__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capitalize&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;casefold&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;center&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;count&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;encode&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;endswith&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;expandtabs&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;find&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;format&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;format_map&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;index&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isalnum&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isalpha&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isascii&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isdecimal&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isdigit&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isidentifier&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;islower&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isnumeric&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isprintable&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isspace&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;istitle&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;isupper&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;join&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;ljust&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;lower&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;lstrip&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;maketrans&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;partition&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;replace&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rfind&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rindex&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rjust&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rpartition&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rsplit&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rstrip&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;split&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;splitlines&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;startswith&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;strip&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;swapcase&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;title&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;translate&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;upper&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;zfill&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;I spy with my little eye, that any string object lets itself be split or joined or lets its case be altered and much, much more!&lt;/p&gt;
&lt;p&gt;I can go into any object I want (with few exceptions) and change its attributes!&lt;br&gt;
For example, I type &lt;code&gt;os.sep&lt;/code&gt; to get the path separator. (on a linux machine it’s &lt;code&gt;/&lt;/code&gt; and on windows it’d be &lt;code&gt;\&lt;/code&gt; and if python ever ran on classic Mac os I guess it’d be &lt;code&gt;:&lt;/code&gt;&lt;br&gt;
I can just change &lt;code&gt;os.sep&lt;/code&gt; to be &lt;em&gt;!&lt;/em&gt; or &lt;em&gt;hahaha&lt;/em&gt; or whatever i want!&lt;br&gt;
Or I could just add whatever attribute I want to any object!&lt;br&gt;
In short, I can set and get attributes on any object I want!&lt;br&gt;
Sounds pretty free-ing! Will have to see what this means in practice!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Aha&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
The inbuilt functions that I have learnt are not the norm.&lt;br&gt;
Most code is written in classes and functions are available as methods of those classes.&lt;br&gt;
Ruby natively works this way.&lt;br&gt;
Python pragmatically, has created some functions as primitives.&lt;br&gt;
(One possible reason could be, for example, rather than have a &lt;code&gt;len&lt;/code&gt; method on every in built class, they musta thought it easier to create a primitive &lt;code&gt;len&lt;/code&gt; function that worked on different classes and data types)&lt;br&gt;
In real life (and across programming languages) I should mostly be looking at object.method(), for e.g. &lt;code&gt;some_list.pop()&lt;/code&gt;&lt;br&gt;
If I want to look at how to manipulate something, the general heuristic is to look at methods first. Does an object support manipulating itself through itself? After all, it knows itself best! :)&lt;/p&gt;
&lt;p&gt;If I want to know what methods an object supports, I can of course call &lt;code&gt;dir&lt;/code&gt; on it, but a better cleaner way is to call &lt;code&gt;help&lt;/code&gt; on it. (&lt;code&gt;help(list)&lt;/code&gt;). It shows, more clearly, the methods an object supports.&lt;/p&gt;

&lt;h3 class="relative group"&gt;Why Classes
 &lt;div id="why-classes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#why-classes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;Do I &lt;em&gt;need&lt;/em&gt; to create classes?&lt;br&gt;
Of course not, I have been creating programs without them all these months.&lt;br&gt;
A couple of reasons I’d create a class,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;To stop kludginess. I don’t to expose a list of dictionaries, that contain more dictionaries that have lists as their values. better to abstract them away from the user (future me) and present simpler interfaces.&lt;/li&gt;
&lt;li&gt;I love to write well crafted prose. Classes represent complete sentences in human language. Nouns and verbs amounting to a sentence that makes sense and is well crafted.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 class="relative group"&gt;How do i create a class
 &lt;div id="how-do-i-create-a-class" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#how-do-i-create-a-class" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Aha&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Just realised that when I create a class, I am creating a new type of primitive for myself.&lt;br&gt;
My own data type!&lt;br&gt;
I can create my own numbering system if i want to!&lt;br&gt;
&lt;em&gt;Muahahahahahahaha&lt;/em&gt;&lt;br&gt;
&lt;em&gt;(comes back to reality)&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;somethingsomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;	&lt;span class="k"&gt;pass&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br/&gt;
That is all I need. 
Just to say class, create a class with this here name! And *Abracadabra-Alakazoom*, Python goes ahead and creates a new class for us! 
&lt;p&gt;As I wrote above, this is a factory, to create new &lt;code&gt;somethingsomething&lt;/code&gt; objects.&lt;br&gt;
I can do that by just assigning it a name. Like Iron man summoning his suit out of thin air.&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;somethingblue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;somethingsomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br/&gt; 
And boom! The object `somethingblue` materialises!
&lt;p&gt;This is just a blank template, of course.&lt;br&gt;
I cannot do much with this. (Right now, I don’t even know, what I can imbue this with, or use it for :P)&lt;br&gt;
So I guess, the rest of the course will probably deal with how to make classes work and function the way I want them to.&lt;/p&gt;
&lt;p&gt;I can add attributes to my classes just by declaring them.&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;somethingblue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;sky&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;somethingblue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;liquid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ocean&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;somethingblue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mh"&gt;0x0000FF&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br/&gt;
And then i could `dir(somethingblue)` and it will give me them attributes, along with a list of everything it has inherited from the parent class (here the `type` class)
&lt;p&gt;A neater way of looking at only what I defined, is to do &lt;code&gt;vars(somethingblue)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Aha&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Heuristic - While i &lt;em&gt;can&lt;/em&gt; add attributes willy-nilly to any object I want to, it’s bad form.&lt;br&gt;
The idea being that the factory (the class) should ideally provide a set of default attributes and values, so that not too much monkeying is required.&lt;br&gt;
This process/template of assigning sane attributes is called a constructor.&lt;/p&gt;
&lt;p&gt;The way we create a class is two fold &lt;em&gt;(&lt;a href="https://en.wikipedia.org/wiki/Smalltalk" target="_blank" rel="noreferrer"&gt;very Smalltalk-esque&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I create a class.&lt;/li&gt;
&lt;li&gt;I add its attributes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So when Python goes to create an object of the class, it does it like quite similarly&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it creates as new object when i say object = class_whatever(), using the &lt;code&gt;__new__&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;it then goes looking to see if there is an &lt;code&gt;__init__&lt;/code&gt; method in the class, and if it exists, then to use that to setup the default attributes&lt;/li&gt;
&lt;li&gt;it uses the &lt;code&gt;self&lt;/code&gt; (by convention) argument, to assign those attributes to the object. (I am applying shampoo to mine own &lt;em&gt;self&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;and &lt;em&gt;then&lt;/em&gt; assigns it to the variable that’s supposed to catch the instance.&lt;br&gt;
&lt;em&gt;(I always somehow assumed it was the other way around. A variable in memory was assigned and then created like a building. This is more nature like. Stuff grows and then is assigned a name)&lt;/em&gt;&lt;br&gt;
I can absolutely go do what I want, assign attributes however, but if I want to stay sane, I’d rather follow convention&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And that’s about all I studied today.&lt;br&gt;
Follow along the my &lt;a href="https://janusworx.com/tags/pythonoop/" &gt;Python OOP train&lt;/a&gt; here.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;P.S. The Feynman Method.&lt;/p&gt;
&lt;center&gt;&lt;iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/tkm0TNFzIeg" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen&gt;&lt;/iframe&gt;&lt;/center&gt;</description></item></channel></rss>