Does different python code execution implements different int arrays? [duplicate] - python

This question already has an answer here:
Different behavior in Python script and Python IDLE?
(1 answer)
Closed 9 months ago.
a = 1000
b = 1000
a is b
So if you run this code using pycharm or command python filename.py a is b = True
If you run this code using command line a is b = Fasle.
I know about implemented array in python (-5 .. 256), but the question is why it's True when you run your code not from command line?

Normally, a is b checks if both variables refer to the same object. And a == b checks for equality of their values. However, that might not be the case in python for small integers. Look at this reddit post with similar question to get more info.

Related

How to ask Python to take a string as variable name [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 months ago.
My situation is as follows: I am testing a Python script where many variables are there, and I want to print out one of the variables controlling from the command line. A toy example is (the script is called toy.py:
import sys
a = 123
print(sys.argv[1])
From the command line I gave: python toy.py a and the output will be a since the a in the command line is taken as string by python. But the expected output is 123, a.k.a I want Python to take the a in the command line as a variable name.
Use gloabals:
print(globals()[sys.argv[1]])
Ref: https://www.programiz.com/python-programming/methods/built-in/globals
Example:
age = 23
globals()['age'] = 25
print('The age is:', age) // prints: The age is 25
You can access global variables using global() and print them via user input. But this is generally very unsafe and a bad idea. In a commercial program, a website for example, this will lead to a major leak in the system. In other words, you are begging the hackers to penetrate your program.
On the other hand, a very useful solution that I will use in these scenarios is to use a dictionary to hold as many variables as I like, and I will index the dictionary using the command line input.
a = 1
b = 2
c = 3
my_vars = { 'a':a , 'b':b , 'c':c}
print(
my_vars[ sys.argv[1] ]
)
There are other solutions using the eval() and exec() functions but those also have the same problem as accessing global().

How to terminate python program with given signal? [duplicate]

This question already has answers here:
Exit codes in Python
(14 answers)
Closed 4 years ago.
In C, I'd do something like
int main() {
return 42;
}
and after executing the program, I can(in Linux) prompt echo $? to the console to get the desired 42 as the return signal from the last operation. How can I do that in python?
I tried just creating a file with a return 42 in it, but it says that it is not inside a function(obviously).
exit(42)
will give you the desired behaviour.

Is there a non-pdb way to enter Python interpreter at a given line? [duplicate]

This question already has answers here:
drop into python interpreter while executing function
(3 answers)
Closed 5 years ago.
Is there a way to cause a python script to enter the interpreter at a given line? I know I can use import pdb; pdb.set_trace() to begin debugging at a certain line, but that means all the pdb commands may get in the way.
Is there a way to kick into the regular interpreter (or ipython) outside of pdb?
The easiest way is code.interact:
import code
code.interact(local={**globals(), **locals()})

Execute File from python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 7 years ago.
Im trying to make a python program, and i just can't find any information on Google on how to just execute a non-python file from python (with an if statement). For Example:
if abc == "abc":
"Execute ThisFile.sh"
EDIT: this was marked as dupe, but I am not trying to run a single command, I want to execute the entire file.
Use subprocess module.
if abc == 'abc':
subprocess.check_output('bash file.sh', shell=True)

Calling Python function from MATLAB [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Call Python function from MATLAB
I need to call a Python function from a MATLAB environment. Is it possible?
Let's assume, I have the following Python code:
def squared(x):
y = x * x
return y
How do I call squared(3) from MATLAB workspace/code and get 9?
I have never done this before, so someone else will have to provide a 'real' answer, but
I can offer this.
I often call the shell from MATLAB on my Linux machine, with an exclamation point, >>!{comm}
So I suppose you could have a shell script that calls python and call the script from MATLAB.
wbg

Categories

Resources