Getting Started with Python and the Spyder 5 IDE (Object Orientated Programming OOP)
The Scientific Python Development Environment (Spyder) has a very similar user interface to the commercial product Matrix Laboratory (Matlab) which is commonly used to teach computer science fundamentals in Universities in the scientific and engineering fields. Unlike Matlab which is an expensive commercial product, Python and the Spyder Integrated Development Environment (IDE) are open-source and can be freely downloaded using the Anaconda Python Distribution. The Anaconda Python distribution contains a number of additional open source data science libraries such as Numeric Python (numpy), the Python and Data Analysis Library (pandas) and the Python plotting library (matplotlib) which can be used to replace Matlab functionality for most purposes in the scientific fields.
This particular video is a beginner guide that will look at importing objects from a user script file (or module) using the dot syntax. Although this video focuses on the concepts behind Object Orientated Programming mainly by working with modules, the analogy is extended to classes and then to modules and packages.
A slightly more detailed written guide is available here:
https://dellwindowsreinstallationguide.com/python-object-orientated-programming/
Note that this video continues on from my other tutorials on procedural programming and code blocks. You should understand these concepts before starting with this video. These videoss and guides are available here:
https://dellwindowsreinstallationguide.com/introduction-to-python-using-the-spyder-5-ide/
https://www.youtube.com/watch?v=JhlZnAYDRG0
https://dellwindowsreinstallationguide.com/introduction-to-python-code-blocks-with-builtin-python-using-the-spyder-5-ide/
https://www.youtube.com/watch?v=2mRBCNm8UUw
Modules can essentially be thought of as other python script files (.py extension). We can import objects from a module object_a by using:
from object_a import object_b
from object_a import object_b1, object_b2
from object_a import *
Alternatively we can import the object and use dot notation to reference objects within it:
import object_a
object_a.object_b
In the English language context, the dot can be thought of as a short hand abbreviation for get object_b from object_a.
Or import objects using an alias:
import object_a as alias
alias.object_b
We start by looking at only our own code. We can reference objects (functions and instances of inbuilt classes) from another module using the notation above. Instances of inbuilt classes (int, float, bool, str instances) can be directly reference. In the case of a function, we are only informed that it is a function when we reference it. In order to call a function, opposed to just referencing it, the function needs to be called with parenthesis which contain the functions input arguments. Details about these will be provided in the functions docstring.
Python has a huge number of inbuilt modules and additional modules available via an Anaconda installation. Many of these are used to routinely carry out additional functionality without writing everything from scratch. On a Windows 10 (default installation location), these can be found in:
%UserProfile%\anaconda3\Lib
I demonstrate using some of the inbuilt basic modules to highlight use of the dot syntax.
This dot notation is also used to reference attributes from within a class. attributes are objects i.e. instances of other classes and can be referenced directly. methods can be thought of as are functions contained within a class and like functions are referenced without parenthesis but called using parenthesis and any input argument. Once again details about these will be provided in the docstring.
More complicated Python code may be packaged. A Python Package is essentially a folder with multiple Python modules. The folder must contain an __init__.py module which is referenced when the folder alone is selected. Assuming we have a package folder1. Using dot notation to reference the folder:
import folder1
will reference the __init__ module within folder1. Other python scripts within the folder can be referenced using dot notation from the folder e.g. if there is a second file called test.py this can be accessed using:
import folder1.test
Python Packages are found as subfolders within:
%UserProfile%\anaconda3\Lib\site-packages
There are also a number of third party modules and packages available to download. I finish up by looking at the third-party package python-docx and explore how classes are being imported from the module using the dot notation and some of the nuances.
To install Anaconda for Windows and Linux see:
https://www.youtube.com/watch?v=0_3zF6Grwn4
https://www.youtube.com/watch?v=50Lp9tQw2cA
To update to Spyder 5 see:
https://www.youtube.com/watch?v=YO3xjM7Ct2w
#Python #programming #Spyder5