Sunday, November 24, 2019

[PythonOOPTutorial] 02 Python Classes, Attributes, Methods and Functions

This is the second article of the Python OOP Tutorials.
If you are new to Python, this is how to install Python on your computer.

In this tutorials we assume you have some basic experience in programming.


CLASSES

Below is how to define a class in Python.

Here "Animal" is the class name. Currently we do not have any attributes or methods in it. So we write "pass" inside it. You can find more about Python empty classes from here. At the end we have created an object using out empty class.
We use the keyword "class" before the class name and ":" symbol after the class name based on Python standards.

You can find more about Python naming conventions from here.


ATTRIBUTES

Classes can contain different attributes which describe the class.
In Python we do not need to define the type of the attribute; it is not mandatory.

Below is how to define attributes and assign values for attributes in Python.

To comment a line you can use "#" symbol in the beginning of the line

When it comes to attributes, there are two types of attributes.
  • Class Attributes - Value common for all the instances of the class
  • Instance Attributes - Values changes based on the instance
If you need more details about Class attributes and Instance attributes please refer this.


METHODS

Classes may contain different methods which serves an specific task.
Below is how to define methods in Python.
Here method name is "sound". We use keyword "def" before method name and ":" symbol after the method name. As the default input parameter we have to pass the object which invokes the method as an input parameter "(self)".

Python has methods and functions. We have to keep that in mind carefully. You can find out more about those differences on methods and functions from here.

When it comes to Python methods;
  1. Method is always associated to an object.
  2. The object which invokes the method, will be passes as a parameter to the method.
  3. Returning data from a method is optional.
There are two types of methods in Python.
  • Instance Methods
  • Static Methods
If you need more details about Instance methods and Static methods, please refer this.


FUNCTIONS

Functions are almost like Methods, but those are not associated with objects. You can define a function in the same way as a method. Functions do not need default input parameters; input parameters are optional for functions.

Here we have a function called printCountry which has an input parameter which is optional. Additionally we have assign that input parameter a value. It will be used when we call the function without passing a parameter.

First we call our function with the argument "India". So it will assign India to the input parameter and print Country name is India.
At third function call we do not pass any argument to the function. So it will assign Sri Lanka to the input parameter and print Country name is Sri Lanka


Those are the fundamentals of Python required for OOP (Object Oriented Programming).
Lets discuss about OOP with Python from the next article.


Cheers...!!




More about Class attributes and Instance attributes..

For example consider we have a class called Animal and it has a Class attribute called numberOfLegs.
So when we assign a value for a class attribute it is same across all its instances.


To change the value we have to access it with the class reference; not with the object reference. If we try to change the value of the class variable using object reference, it will create a Instance attribute with the same name.
Here initially there are no instance attributes called numberOfLegs. So animal1 and animal2 objects will display the value as 4. 

Then we change the value of numberOfLegs for animal1 object. So what will happen is instance attribute will be created for animal1 object and its value will be 3.

When we ask for the value of any attribute; system will check whether there is any instance attribute with the same name; if a matching instance attribute found, system will return that value.
If there is no matching instance attribute found, then system will check for class attributes.

At animal1 object, system will find the instance attribute and return its value 3.
At animal2 object, system will cannot find a instance attribute and then checks for a class attribute. Then system find the class attribute and return its value 4.


More about Instance methods and Static methods..

Usually we write methods inside classes where at least one input parameter is required are called Instance methods. Static methods are bit different from instance methods.
On static methods we do not need the default self parameter. To make a static method we have to use the @staticmethod annotation before the method definition. So that will make a static method.
We can call static methods by the class reference too.

Ex: here we have called the static method by creating an object reference. 
animal = Animal();
animal.staticMethod1();

Other than that, we can directly call the static method using the class reference too.
Animal.staticMethod1();
This will also provide the same output.

No comments:

Post a Comment