
{"id":15,"date":"2025-09-09T00:01:00","date_gmt":"2025-09-09T04:01:00","guid":{"rendered":"http:\/\/joeybernard.ca\/?p=15"},"modified":"2025-09-08T12:39:12","modified_gmt":"2025-09-08T16:39:12","slug":"python-intro-to-classes","status":"publish","type":"post","link":"https:\/\/joeybernard.ca\/index.php\/2025\/09\/09\/python-intro-to-classes\/","title":{"rendered":"Python &#8211; Intro to Classes"},"content":{"rendered":"\n<p>Welcome to the first of what will be a series of articles talking about what is great about Python, and what is not so great. Even though I am a fan of the language, I am not so enamored as to be blind to its weaknesses. So over the coming months we will not only learn about how to do certain tasks in Python, but also what pitfalls you may run into during your travels. Once we have some Python basics under our collective belts, we will start looking at ways to apply this new found knowledge and code some programs up for the Raspberry Pi. The Raspberry Pi is one of the low cost educational boards out there that you can experiment on. The first subject we will look at is using objects in Python. According to the official documentation, objects are Python\u2019s abstraction for data. All data in a Python program is represented by objects or by relations between objects. Actually, almost everything in Python is an object. So learning a bit more about how they behave is important to developing code of any real value. More specifically, we will look at classes and how to work with objects of your own design.<\/p>\n\n\n\n<p>Classes in Python support all of the most common concepts in Object Oriented Programming. The mechanisms of classes in Python were inspired by C++ and Modula-3. A class can inherit from multiple base classes and can override any methods in the base classes. All of the methods in the base classes are also available to be called from the inheriting class. As for data, objects can contain arbitrary amounts and types. And because Python is a dynamic language, all of this is modifiable at run-time. As an example, let\u2019s say that you want to develop a program that will do some kind of geometric processing. One of the core objects that you will want to use is likely something that represents a point. Looking at two dimensional geometry first, you will need to store two values, an x and a y. In code, this would look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   class Point:\n      pass\n<\/code><\/pre>\n\n\n\n<p>This gives us a blank class called Point. The first line is how you define a new class. It is very similar to how you define a new function, except that you exchange the keyword \u2018def\u2019 with the keyword \u2018class\u2019. A class always needs to have at least one statement. If you don\u2019t want your new class to do anything yet, you can use the keyword \u2018pass\u2019. This essentially tells Python, \u201cdon\u2019t do anything here\u201d. You can now use this class to create new objects of type Point. You can do this with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   my_point = Point()\n<\/code><\/pre>\n\n\n\n<p>This is not really all that useful yet. There is nowhere to store our point values. Or is there? One of the really cool things about Python objects is that they are fully dynamic. You can add x and y values with the statements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   my_point.x = 10.0\n   my_point.y = 20.0\n<\/code><\/pre>\n\n\n\n<p>This particular instance of the Point class now has an x and y value. Unfortunately, we can\u2019t really use them effectively. It would be better to have them as part of the definition of the class so that we can write methods that know how to use this data. It many cases, it also makes sense to initialize these variables. One way to do this is to simply write statements within the class definition directly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   class Point:\n      x = 0.0\n      y = 0.0\n<\/code><\/pre>\n\n\n\n<p>The other way to initialize values is to use the&nbsp;<strong>init<\/strong>&nbsp;method in classes. This method gets automatically called when a new object gets instantiated. This looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   class Point:\n      def __init__(self):\n         x = 0.0\n         y = 0.0\n<\/code><\/pre>\n\n\n\n<p>You can then set your values for x and y after creating a new instance with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   my_point = Point()\n   my_point.x = 10.0\n   my_point.y = 20.0\n<\/code><\/pre>\n\n\n\n<p>While this works well, one thing to remember is that programmers are inherently lazy and do not want to type more than absolutely necessary. Following this idea, it would be great if you could assign your x and y values at the same time you are creating a new object. In Python, you can. The&nbsp;<strong>init<\/strong>&nbsp;function can be written to take parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   class Point:\n      def __init__(self, xvalue, yvalue):\n         self.x = xvalue\n         self.y = yvalue\n<\/code><\/pre>\n\n\n\n<p>So your code simplifies down to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   my_point = Point(10.0, 20.0)\n<\/code><\/pre>\n\n\n\n<p>Isn\u2019t that much easier? Now that you have a point defined, you can add methods to work on this data. One of the first methods for a point is its absolute value. To add this to your class, you can just add the function definition to the class definition with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   class Point:\n      def __init__(self, xvalue, yvalue):\n         self.x = xvalue\n         self.y = yvalue\n      def abs_val(self):\n         sqr_val = self.x**2 + self.y**2\n         return sqr_val**0.5\n<\/code><\/pre>\n\n\n\n<p>Now that you have a basic two dimensional point defined, you may want to create objects that can handle three dimensional points. Code reuse is something that should be aimed for, and easy to do in Python. You can create a new class that builds on the existing code for two dimensional points. This would look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   class ThreeDPoint(Point):\n      def __init__(self, xvalue, yvalue, zvalue):\n         self.x = xvalue\n         self.y = yvalue\n         self.z = zvalue\n<\/code><\/pre>\n\n\n\n<p>Written this way, the initialization method has been overridden to take three parameters rather than two. But, nothing has been done to the method abs_val, inherited from the Point subclass. So when you call this from an instantiated ThreeDPoint object, you will get the length of the x and y parts of this point. If you want a proper three dimensional absolute value, you will need to override the abs_val function.<\/p>\n\n\n\n<p>Now that you\u2019ve seen how to create your own objects, next month we\u2019ll look at some details around how Python manages these objects in memory. On most desktop systems, memory is not really a concern. But the Raspberry Pi is relatively constrained, so knowing how memory is handled will become more important for your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the first of what will be a series of articles talking about what is great about Python, and what is not so great. Even though I am a fan of the language, I am not so enamored as to be blind to its weaknesses. So over the coming months we will not only &hellip; <a href=\"https:\/\/joeybernard.ca\/index.php\/2025\/09\/09\/python-intro-to-classes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python &#8211; Intro to Classes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[5,4],"_links":{"self":[{"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":1,"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/posts\/15\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joeybernard.ca\/index.php\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}