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

  • #1
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
  • #2
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 member 428835
  • #3
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 member 428835
  • #4
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 member 428835
  • #5
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 pbuk and member 428835
  • #6
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 The Bill and member 428835

Similar threads

Replies
4
Views
3K
Replies
4
Views
306
Replies
11
Views
4K
Replies
5
Views
1K
Replies
3
Views
2K
Replies
2
Views
3K
Replies
5
Views
2K
Back
Top