Can I run sections of code independently in Python and C++ like in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    C++ Matlab Python
Click For Summary

Discussion Overview

The discussion revolves around the ability to run sections of code independently in Python and C++, similar to how it is done in MATLAB. Participants explore the differences between interpreted and compiled languages, and how these differences affect code execution and modification.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that MATLAB allows for sections of code to be rerun independently due to its interpreted nature.
  • It is suggested that Python, being an interpreted language as well, should allow similar functionality, although specific methods are not detailed by all participants.
  • Others argue that in C++, which is a compiled language, any changes require recompilation, making it less flexible for running sections of code independently.
  • A participant mentions that running scripts from the command line executes the entire script each time, contrasting with interactive sessions that allow for line-by-line execution.
  • Another participant highlights the use of Jupyter notebooks in Python, which enable independent execution of code cells, suggesting this is a common practice in data science.
  • It is noted that C++ can be integrated with Python through wrapper libraries, allowing Python code to leverage C/C++ performance while maintaining Python's flexibility.

Areas of Agreement / Disagreement

Participants generally agree that MATLAB and Python allow for more flexible code execution compared to C++. However, there is some disagreement regarding the extent to which Python can replicate MATLAB's functionality, and whether running sections independently is universally applicable across all contexts in Python.

Contextual Notes

Some participants express uncertainty about specific methods for executing sections of code in Python, and there is a lack of consensus on how these practices compare to MATLAB's capabilities.

Who May Find This Useful

This discussion may be useful for programmers transitioning from MATLAB to Python or C++, as well as those interested in understanding the differences in code execution between interpreted and compiled languages.

member 428835
Hi PF!

I typically code in MATLAB. Here, if I run the following program
Code:
%% Section 1
x = 5;
y = 2;
%% Section 2
z = x + y;

everything works. But now let's say I wanted to change line 5 to z = x - y. In MATLAB this is simple: I can simply change line 5 and rerun section 2. Is there a way to do this in Python and C++, or do I have to rerun the entire program?

Thanks for your help!
 
Physics news on Phys.org
MATLAB and Python are interpreted languages. You can change the code whenever you like. A program reads and executes that code.

C++ is compiled : a program called a compiler takes the source code and produces unreadable machine code that then may be executed by the CPU. Usually in order to change anything you have to recompile it, though there are special programs called debuggers that you can use to stop the program and read data.

Compiled code executes more quickly, but if you are spending most of your computer time in someone else's routines then it may not matter much.

So I would suspect you can easily do what you want to do in Python, but I seldom use it so I don't know how. Can't do it in C++.
 
  • Like
Likes   Reactions: member 428835
joshmccraney said:
In MATLAB this is simple: I can simply change line 5 and rerun section 2
Because Matlab is interpreted, as is Python. That means they can be changed at run time.

C++ is compiled so requires recompilation for changes.

EDIT: I see Hornbein beat me to it
 
Last edited:
  • Like
Likes   Reactions: member 428835
While you may be able to rerun section 2 in a Matlab program, in other languages interpreted or otherwise you'll need to run the entire program from start to finish.

This is one of the reasons programmers break up code into separate steps (programs) with intermediate files to allow rerunning one section on long runs or to restart when an error occurs like a lost network connection or out of disk space condition.

In truth, I didn't know you could rerun a portion of a Matlab program. I've always rerun from the beginning.
 
  • Like
Likes   Reactions: member 428835
There is also a difference between running a script from the command line (which runs the entire thing each time) as opposed to using an interactive session (where you can step through line by line and change things or repeat things as you go).
 
  • Like
Likes   Reactions: pbuk and member 428835
In data science, Python is often used via Jupyter notebooks. These consist of cells of code which can be executed independently. So yes, what you are asking is not only possible but also common.

Jupyter works with multiple languages. Actually, its name is kind of a wordplay on them: Julia, Python and R.

As the others have written, C++ is a completely different beast. If Python programmers need the speed provided by C or C++, they mostly use wrapper libraries which are called like normal Python code but use C/C++ under the hood.
 
  • Like
Likes   Reactions: The Bill and member 428835

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K