Python being a general-purpose programming language supports multiple programming paradigms, viz procedural, functional, and object-oriented programming (OOP). Each Pythoneer often uses a combination of these programming styles and usually has her preferred style of coding. As a Python programmer, you can write code in a style that you like.
Considering the number of concepts that OOP encompasses and its popularity, it demands more than one article. However, I will limit the discussion to some of the most widely used object-oriented programming concepts here.
Difference between Procedural programming and Functional programming
Procedural programming is the one we learn when we start programming. In its simplest form, procedural programming takes the top-down approach of executing code. The code will be executed line by line sequentially in an order it has been written. That's it, that's procedural programming for you.
If you learn by example, here it is:
print(‘First, this line will execute.')
print(‘Next, Python executes this line.')
print(‘Then, this line shows up.')
print(‘Finally, Python completes execution by printing this line.')
Below is the output:
First, this line will execute.
Next, Python executes this line.
Then, this line shows up.
Finally, Python completes execution by printing this line.
Instead of print
statements, we can have any code. No matter what code, Python will execute it. In case, the Python interpreter cannot execute the code, it will throw the appropriate error and will finish the execution abnormally. I can say it is a pretty easy programming style.
Next comes the functional programming style. Here, we try to combine code lines into logical blocks that can be reused as and when required.
Say you want to backtest a strategy and write a Python code for it.
The steps to do so usually involves:
- Downloading the historical data
- Calculating buy and hold returns
- Computing the statistical or technical indicators
- Generating trading signals
- Calculating strategy returns and other evaluation metrics
- Visualising the performance of the strategy
Each of the above-listed steps can take one or more lines of code to achieve the defined objective. You can use either approach, procedural or functional, both work. However, the focus, here, would be to understand functional programming. We can create a dedicated function that encapsulates one or more steps defined above.
Below is an example workflow involving various functions to backtest a given strategy:
How many functions should be created and what function performs what functionality generally depends on the coder and how the problem statement is being approached.
Stay tuned for the next installment in which Jay will discuss why a grouping of functionality is preferred.
Visit QuantInsti to read more about this research: https://blog.quantinsti.com/object-oriented-programming-python/.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.