diff --git a/_articles/Inheritance-and-Polymorphism.md b/_articles/Inheritance-and-Polymorphism.md new file mode 100644 index 0000000..0090347 --- /dev/null +++ b/_articles/Inheritance-and-Polymorphism.md @@ -0,0 +1,50 @@ +--- +layout: page +name: Inheritance And Polymorphism +topic: Python +updated: April 30 2026 +articleBefore: Object Oriented Programming +articleAfter: none +--- + +{% include articleHead.md %} + +## Two Large Words + +Inheritance, not the kind when someone passes away, is when an Object can Inherit the properties of another one. +Polymorphism, try saying that 3 times fast, is when an Object that Inherited properties can be transformed into its parent Object. + +## Two Large Explanations Part 1: Inheritance + +The name actually lends itself quite well to the analogy here: Person A is the child of Person B, who is in turn the child of Person C. Here is a visual: +C --> B --> A +In typical, real world inheritance, C's assets go to B, which then go to A. Only one person at a time has them. In code, the same idea applies but the ownership is not exclusive. Let's apply some properties to A, B, and C: +### C +- Favorite Cheese +- Lease Favorite Cheese +### B +- Name of Pet Duck +### A +- Aglet Color +- Shoe Sole Material + +OK, now that everyone has properties, lets demonstrate programmatic inheritance! +C, the highest level of this inverse pyramid, only has the two properties directly on them: Favorite and Least Favorite Cheese (Gouda and Provolone respectivley). Easy so far +B, the middle layer has access to its properties, Name of Pet Duck (Gerald), and the two of the Object they inherit from, Favorite and Least Favorite Cheese (Calderwood and Kunik respectivley). +A, the last in the chain, has access to all thos above in addition to its own. Favorite Cheese, Oma. Lease Favorite Cheese, Verano. Name of Pet Duck, Subaru. Aglet Color, neon pink. Shoe Sole Material, depleted uranium. + +An Inheriting Object gains access to all (pin here for the exceptions section!) properties of those it Inherits from, all the way up the chain to the top. The final thing to note with Inheritiance is an Object can (typically) Inherit from one other Object at a time - Python allows this, it will be touched on later. + +## Polymorphism + +It's time for the scarier word! Polymorphism is a *very* fancy way of saying that an Object that Inherits from another Object is that other object. This is best phrased as such: A 2002 Honda Civic IS A car IS A vehicle. The Inheritance chain here is: +Vehicle --> Car --> 2002 Honda Civic +Polymorphism doesn't change anything about Inheritance, so all the Inheriting rules above apply. It instead extends it: Lets have our pretend 2002 Honda Civic get some cleaning. We take it first to a 2012 Toyota Corrolla Wash. No luck, a 2002 Honda Civic is not a 2012 Toyota Corrolla, we'll need to go somewhere else. Now we find a Car Wash. Ah, we are in luck! A 2002 Honda Civic IS A car, so it can go into the Car Wash! As we enter, we also see a 2012 Toyota Corrolla exit, - a 2012 Toyota Corrolla IS A car as well. Now lets trade in the newly cleaned car for a brand new 7000 Series L Train from Chicago. This thing also needs cleaning, so we take it to the Car Wash - a Train IS NOT A Car, so no dice. We are in luck though, we find a Vehicle Wash! A Train IS A Vehicle, so this great 7000 Series can get clean. As can the Plane that entered after. + +Directly applying to code, any Object that Inherits from another Object is the other object - its Type is both its own (2002 Honda Civic), and the Inherited Type (Car). This means a variable of type Car can hold any Object that Inherits Car, regardless of what its specific make or model is. The catch, as there always is one, is only properties that all Cars have are accessible - without knowing if you have the features of a Bugatti Veyron or Model T. + +## Exceptions + +They always exist. Starting with multiple Inheritance - Python does allow this to happen. Some languages don't (ex. C#, Java), some do (ex. Python, C++). The same principles apply with multiple Inheritance as with single Inheritance, the child Objects just get more properties and can be Polymorphed into more things. + +Next on the chopping block, what actually gets Inherited. When declaring a variable or function in a Class, you can define a privacy level: public, protected, or private (there are more than this, but these are by far the most common and least confusing). Public means anyone can access this property, both from within the Object and from outside via an Instance. Protected means only the Object and those that Inherit from it can access that property, it is not available in the Instance. Lastly, private means only that Object can access it, not Inheriting Objects, not Instances. \ No newline at end of file diff --git a/_articles/Object-Oriented-Programming.md b/_articles/Object-Oriented-Programming.md new file mode 100644 index 0000000..8de2713 --- /dev/null +++ b/_articles/Object-Oriented-Programming.md @@ -0,0 +1,26 @@ +--- +layout: page +name: Object Oriented Programming +topic: Python +updated: April 30 2026 +articleBefore: Python Basics +articleAfter: Inheritance and Polymorphism +--- + +{% include articleHead.md %} + +## OOO: What is Object Oriented Programming: The Short Version + +Object Oriented Programming: A way to structure code such that distinct functionalities are contained within their own Objects. That is a lot of words to say, simplified, things that need to be used together are together in code. Objects in code are defined in Classes (not the school kind). + +## OOO: What is Object Oriented Programming: The Long Version + +When baking cookies, you don't put everything into a bowl at once, mix it, and use the sun to bake the dough in the bowl. If you do, those must be some funky cookies. Cookies require multiple different tools, or *objects*, when being made. Each object has its own purpose, and the logic to run that purpose: an oven bakes things and knows how to do that. A bowl holds things, and knows how to do that. But a bowl doesn't know how to bake, so it cannot. Now to turn this cookie analogy into code: an Object (capitalized for emphasis) is a tool that has a purpose, and contains the logic and data required to do that purpose. An Object called "ScreenColorChanger" has the purpose of changing the color of the screen, and would contain the logic to do so when called. + +Now for the hard part: Objects within Objects. Back to cookies (go bake some after reading this), an oven does bake things. But the oven itself is also made up of pieces that have their own, more specific purposes. The coils that produce heat, the temperature sensor that reads how hot the inside is, the display that tells the humans how hot it is, and many many more. Objects in code, same thing. The ScreenColorChanger does know how to change the color of the screen, but it is made up of smaller parts that have more specific tasks. One Object contained within could be "PixelColorChanger" - an Object whose sole purpose is to change the color of a single pixel on the screen. ScreenColorChanger doesn't know how PixelColorChanger does its thing, all the ScreenColorChanger needs to know is that PixelColorChanger works and by telling it "Blue" that pixel is now blue. This nesting of Objects within Objects can go quite deep, though there are practical limits - the more Objects the more things need to be kept track of at once by the programmer and the more memory is used on the computer. + +That PixelColorChanger Object seems useful... Good thing it can be used elsewhere outside of ScreenColorChanger too! Objects are not bound to only being used in one place - they can be used aywhere their definition can be found (for better or worse, this can lead to massie spaghetti). Lets add a new Object to the pile here: "ImageRenderer". Rendering an image to the screen does require changing the color of pixels, so we will include PixelColorChanger in ImageRenderer - no need to rewrite that same code if we aready did for ScreenColorChanger! + +This does bring up a question though: are the PixelColorChangers in ScreenColorChanger and ImageRenderer the same Object? The answer......... no! They are not the same Object! Every time you use an Object you are making a new Instance (again, capitalized for emphasis) of it. Each Instance uses the same template (the Object you wrote), but the values contained within are entirely disconnected (yes, person who said "static", we will get to that). Lets use cars as the analogy this time, I'm no longer hungry. If you and your friend both by the exact same model, color, and trim of 2002 Honda Civic, you did not buy the same car. You sitting and driving your car has no effect on your friend doing the same in theirs - the same thing applies with Objects and their Instances. Each Instance is separate from the others, but is spawned off the same base template. In general, an Object can have as many Instances as you want, right up until your computer crashes from being out of memory. + +Now it is time for the excepions to the rules. Starting with the second most recently mentioned: each Instance being entirely separate from the others. That's not entirely true if you use something called a static variable. Statics contain the same value across all Instances - this can be useful for things that need all Instances to have the same information and there is no other way to reach it. The next exception is Objects having as many Instances as you want - unless you have what is called a Singleton. Singltons are Objects where only one Instance can exist at a time. The wonderful third exception: thats all folks there isn't a third. \ No newline at end of file diff --git a/_articles/python-Basics.md b/_articles/python-Basics.md index fa21a38..4476bd4 100644 --- a/_articles/python-Basics.md +++ b/_articles/python-Basics.md @@ -4,7 +4,7 @@ name: Python Basics topic: Python updated: Dec 2 2025 articleBefore: none -articleAfter: none +articleAfter: Object Oriented Programming --- {% include articleHead.md %} @@ -15,7 +15,7 @@ Python is a high level dynamically typed language which is widely used for data ## High Level vs Low Level -What is a high level language? What makes one high level? A high level language is one that is designed, primarily, for human readabiliy ([https://en.wikipedia.org/wiki/High-level_programming_language]). A high level language may read like a sentence, and needs to be translated into the actual 1's and 0's that the computer itself understands. Soem popular high level languages are JavaScript, Java, and of course, Python. +What is a high level language? What makes one high level? A high level language is one that is designed, primarily, for human readabiliy ([https://en.wikipedia.org/wiki/High-level_programming_language]). A high level language may read like a sentence, and needs to be translated into the actual 1's and 0's that the computer itself understands. Some popular high level languages are JavaScript, Java, and of course, Python. On the opposite end of the spectrum, you have low level languages. These are your C, C++, Rust, and Fortran's of the world. They forego human readability in favor of more direct, and faster, processing. The speed increase comes from the or fewer number of translation layers - low level languages only need a few steps to be binary. @@ -25,6 +25,10 @@ Pop quiz, what is the lowest level language? At its core, all computers run in b No, not typing as in typing on a keyboard. Type in programming refers to what a certain piece of data is - is it a number, text, true/false, or something else? It's kinda like the shape of the data, it defines what can fit into that bucket and how it can be used. -Static typing is when the type (shape) of the data is set by the programmer explicitly. That data can only be that one shape, and trying to shove a different shape into it will cause problems. For example, defining data `x` to be a number, I can assign it the value `1`. But if I try to assign itt `"cake"`, there will be an error because "cake" is not a number. And if I take `x` (currently `1`) and try to add `"1"` (the actual character `"1"`, not the number), it will also error. +Static typing is when the type (shape) of the data is set by the programmer explicitly. That data can only be that one shape, and trying to shove a different shape into it will cause problems. For example, defining data `x` to be a number, I can assign it the value `1`. But if I try to assign it `"cake"`, there will be an error because "cake" is not a number. And if I take `x` (currently `1`) and try to add `"1"` (the actual character `"1"`, not the number), it will also error. -Dyamic typing is a lot looser. Types still exist, but they are not defined ahead of time. The data takes the shape that is needed at the time and can be changed on the fly. Instead of defining `x` to be a number, `x` is just defined as data. `x` can be assigned to `1` with no issues. If `x` is then assigne to `"cake"`, the type of `x` will change to text. The value of `x` will be `"cake"` even though it started as a number. Now, let's set `x` back to `1` and see what happens when we try adding `"1"`. We get `"11"`! Errors like this in a dynamically typed language are more common as the computer won't check to ensure the data has the right shape before using it. \ No newline at end of file +Dyamic typing is a lot looser. Types still exist, but they are not defined ahead of time. The data takes the shape that is needed at the time and can be changed on the fly. Instead of defining `x` to be a number, `x` is just defined as data. `x` can be assigned to `1` with no issues. If `x` is then assigned to `"cake"`, the type of `x` will change to text. The value of `x` will be `"cake"` even though it started as a number. Now, let's set `x` back to `1` and see what happens when we try adding `"1"`. We get `"11"`! Errors like this in a dynamically typed language are more common as the computer won't check to ensure the data has the right shape before using it. + +## Data Analytics, Processing, Machine Learning, and Automation Scripts + +The choice to use Python over another programming language comes down to picking the right tool for the job. Want something fast, fast, and even more fast? C or C++ (or Rust) are your friends. Human readable and lots of community support? Python! There are extensive libraries and packages for Python to do just about anything you can imagine. Thats not to say every use case as a library that does it in one line though, a programmer is still needed to put these puzzle pieces together. \ No newline at end of file