次の方法で共有


Part 4: Get started with Python: Object Oriented Approach

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 5: Get started with Python: Debugging in PTVS

Part 6: Get started with Python: Build your first Django Application in PTVS

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:

  1: class shoppingCart:
  2:     def size(self):
  3:         #some code here
  4:     def color(self):
  5:         #some code here

Now that the class has been defined, you can easily declare as many instances of the class as you want. For example:

  1: cart1 = shoppingCart()
  2: cart2 = shoppingCart()

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.

  1: class shoppingCart:
  2:     def size(self):
  3:         #some code here
  4:     def color(self):
  5:         #some code here
  6:     def set_name(self,name):
  7:         self.name = name

Now if we decide to use this class:

  1: cart1 = shoppingCart()
  2: cart2 = shoppingCart()
  3: cart1.set_name(“Bob’s cart”)
  4: cart2.set_name(“Joe’s cart”)

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:

  1: class shoppingCart:
  2:     def __init__(self):
  3:          self.size = “medium”
  4:          self.color = “green”
  5:          self.name = “Average cart”
  6:     
  7:     def cart_size(self,size):
  8:         self.size = size
  9:          def cart_color(self, color):
  10:          self.color = color
  11:     def set_name(self,name):
  12:         self.name = name
  13:     def explain_myself(self):
  14:         print “I am a” + self.size + “shopping cart, ” + self.color + “ in color. I am called ” + self.name

Now as you can see, we have defined the basic properties under __init()__. If we now use it as follows:

  1: cart1 = shoppingCart()
  2: cart1.explain_myself()
  3: cart1.cart_size(“small”)
  4: cart1.cart_color(“blue”)
  5: cart1.explain_myself()

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:

  1: class SubClassName (ParentClass1[, ParentClass2, ...]):
  2:    class_suite

Summary

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!