This is a tutorial series which will teach you how to code in Python. We will start with the absolute basics of installation of Python in Windows and Python Tools in Visual Studio. We will then go through basic constructs in Python and write a couple of programs to summarize what we have learned. We will end with an Object Oriented Approach using Python and a specific feature of Python Tools in Visual Studio: Mixed mode C/C++/Python Debugging
Part 1: Get Started with Python summarized the steps involved to setup Python and Visual Studio for Python Development. We essentially learned how to install Python in Windows, Visual Studio and Python Tools for Visual Studio.
Part 2: Get Started with Python took you through the basics in programming constructs such as output, input, variables and control flow statements including conditional statements and loops including while and for. Using these tools was enough to get started with coding basic applications in Python.
Part 3: Get Started with Python: Functions and File Handling, as the topic suggests, dealt with functions and file handling in Python. We went through the ‘def’ construct used in function definition and file handling constructs for open. read and write. Essentially, by this topic we covered the basics of programming in Python.
Part 4: The Object Oriented Approach
Welcome to this week’s edition of Getting Started with Python! In this section, we adopt the Object Oriented Approach. Python has been designed as an Object Oriented Language. This is quite a broad topic and therefore, we will cover the basics of Objects and Classes and how they are used in Python.
To know what an object is, we first need to know about Classes. In Python, a “class” is basically a set of statements which perform a specific tasks. A class can contain variables and functions. These “classes” serve as a templates from where specific instances of the classes are spawned. These specific instances of the class are referred to as Objects. Each object has specific properties or pre-defined functions also known as methods.
In Python, once a class has been defined, you can have as many instances of the class as you like. And each instance is an object with its own independent properties. This is useful in certain situations like when you have to define two snakes but they have different properties (for example: venomous and non-venomous)
So why do we need classes?
Classes help to keep code modular. You can easily define a class in one file and use it in another. This also helps to make everything simpler.
Now let’s work on with an example:
Now that the class has been defined, you can easily declare as many instances of the class as you want. For example:
Here cart1 and cart2 are called objects. You can also access all object methods and properties. More importantly, each object is independent of the other. Now let’s modify our main class to demo that.
Now if we decide to use this class:
cart1.name now contains “ Bob’s Cart’ while cart2.name contains ‘Joe’s Cart’.
NOTE: You can also just use cart1.name = “Bob’s cart” too.
Now let’s work on constructors. In the above example, the class method was called directly. However it’s also possible to automatically execute a function when the class is called to create a new object, the idea being to initialize the object. This is called as a “constructor” and in order to use it, we also need to use “__init()__”
So now with that information, let’s start to modify our initial class again:
Now as you can see, we have defined the basic properties under __init()__. If we now use it as follows:
If you see the output for each, you will notice that the first output is basically “I am a medium shopping cart, green in color. I am called Average cart.” While the second one would be “I am a small shopping cart, blue in color. I am called Average cart”!
Great! Now you are equipped with the Object Oriented approach. Please note that this only provides enough insights for you to get started with object oriented programming. As already mentioned, this topic is very broad and has quite a few advanced concepts such as inheritance, which basically gives you the option of not creating a class from scratch but ‘inheriting’ the properties of a pre-existing class. The syntax of inheriting a class is as follows:
In this tutorial, what we have gone through is the basics of object oriented programming. I would strongly urge you to go ahead and practice some programs. Why not try and write a simple program using the object oriented approach to display the local time, the time in Vancouver or Calgary or any other place. Keep in mind, that all basic libraries are already included in Python.
Next Part, we will focus on how to use a very specific feature in Visual Studio called Mixed Python/C++ debugging. Tune in, code in Python in the meantime and stay connected!