Comparison of Python Class and Matlab Class | applied electronics engineering

Latest

Comparison of Python Class and Matlab Class

By Applied Electronics - Tuesday, February 14, 2017 No Comments
In the following we show comparison of python class and matlab class. That is the syntax and examples of python class and matlab class and object. This helps you in differentiating classes and objects in Python and Matlab programming languages.

File Extension

In python programming language a class is written and saved in a file with extension .py. The file is also known as module. So classes in python are called modules although module refers to any file with file extension .py. In Matlab programming language a class is written and saved in a file with extension .m.

Example of Class and Object in Matlab


Consider we want to make a class called player with name and age attributes. The following is the class implementation in Matlab.

classdef player
    properties
        name;
        age;
    end
    methods
        function pObj = player(val1, val2)
            if nargin == 2
                pObj.name = val1;
                pObj.age = val2;
            end
        end
    end
end

This code was explained in User defined class and object in Matlab.

Creating object player1 of the class player in Matlab is as follows:

>> person1.name = 'jon';
>> person1.age = 20;

To view the attributes and methods we use the properties and method functions.

>> properties(player)

Properties for class player:

    name
    age

>> methods(player)

Methods for class player:

player

To know more about how this see Properties and Methods of Object Oriented Programming Class in Matlab. 

 Example of Class and Object in Python


Next consider the same class player with same attributes name and age implemented in Python programming language.

class player:
    name = None
    age = None
    
    def __init__(self,var1,var2):
        self.name = var1
        self.age = var2


Or, we could also have written in the following way,

class player:
    
    def __init__(self,var1,var2):
        self.name = var1
        self.age = var2

This is without declaring attributes name and age ahead. The variables attributes can be specified at the time of construction.

Then we create object player1 of the class player in Python as follows:

>>> import player
>>> player1 = player
>>> player1.name = 'jon'
>>> player1.age = 20

To view the properties and methods in Python we use the dir() function as follows.

>>> dir(player1)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'age', 'name', 'player']

Python list all the attributes and method available for the object player1. This is because in Python the class player is an object with default method __init__().

So this showed you example of python class and matlab class for comparison purpose.

Some of the differences and similarities can be drawn. Matlab uses classdef keyword to define class and it end is indicated by the end keyword. Python uses class keyword followed by the class name and then a colon : to define a class.  The indentation in the next line marks the start of class block code. In Matlab the data or variable attributes has to be specified within properties block with corresponding end keyword whereas such block is optional in Python. Matlab uses the constructor for initialization of object. Python also has object initialization constructor through the __init__ method.

No Comment to " Comparison of Python Class and Matlab Class "