I want print paramater's name in a python function [duplicate] - python

This question already has answers here:
How to print actual name of variable class type in function?
(4 answers)
Getting the name of a variable as a string
(32 answers)
Closed 3 years ago.
I want print paramater's name in a python function
First, a variable is assigned a value.Then, pass the variable to a function.
Last,I want to print str(variable) inner the function.
varibale = 1234
def f(x):
print(....)
return
f(varibale)
Expected output is print out the 'varibale' whatever variable is.
if a = 1 ==> f(x), expected output is 'a';if b = 2 ==> f(x), expected output is 'b'.....

Why do you want to print out the value using a function?
If you know the name of the variable, you can simply write your requested function output yourself by just putting 'variable'
So in your example: instead of using f(x), just use print('x')

Related

Obtaining a string version of the variable name in Python [duplicate]

This question already has answers here:
Getting the name of a variable as a string
(32 answers)
Closed 1 year ago.
Consider the following code:
x,y = 0,1
for i in [x,y]:
print(i) # will print 0,1
Suppose I wanted instead to print:
x=0
y=1
I realise f-strings can be used to print the intermediate variable name:
for i in [x,y]:
print(f"{i=}") # will print i=0, i=1
However, I am interested in the actual variable name.
There are other workarounds: using eval or using zip([x,y], ['x', 'y']), but I was wondering if an alternative approach exists.
I think this achieves what you want to do -
for i in vars():
print(f'{i}={vars()[i]}')

Variable of Variable in Python [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 3 years ago.
I've a list of variable names, how can I print value of it using for loop..
var1="First"
var2="Second"
list=["var1","var2"]
for var in list:
print(var) # I want to print value of var,
present output
var1
var2
What I want is
First
Second
list=["var1","var2"]
You've made a list of strings, not variable names. What you need is
list=[var1,var2]

Python - Modify an input variable inside a function [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Why can a function modify some arguments as perceived by the caller, but not others?
(13 answers)
Closed 3 years ago.
The following code:
def function(X):
X.upper()
if X == 'YES':
print ('success')
else:
print ('fail')
function('yes')
Produces:
fail
But this code:
def function2(X):
Y = X.upper()
if Y == 'YES':
print ('success')
else:
print ('fail')
function2('yes')
Gives me:
success
Why is this? I want to be able to edit my input variables within my functions. Is there a more efficient way to do this than copying variable values to new variables? I'm running Python 3.7.1.
Thanks!
Because "".upper() returns new string, it doesn't change the original. Strings are immutable in Python.

How to get the value of a variable by executing a string? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I have the following code:
a=2
string="a"
b=exec(string)
print(b)
Output:
None
I want b to have the value of 'a' i.e. 2 how can I do that?
If I understand your case correctly, you want to evaluate some python source from string in context of existing variables.
Builtin eval function could be used for that.
a=2
string="a"
b=eval(string)
print(b)
Anyway. Why do you need that? There is better way to do that for sure.
Probably in your case you could use dictionary to remember values instead of separate variables. And after reading "names" from file use this names as dictionary keys.
your_dict = {}
your_dict["a"] = 2
string = "a"
b = your_dict[string]
print(b)
a=2
string="b=a"
exec(string)
print(b)
exec() just exectues the code in the given string, so the assignment must be done in the string itself.

Changing function's default parameters in Python [duplicate]

This question already has answers here:
Is it possible to change a function's default parameters in Python?
(4 answers)
Closed 6 years ago.
Let's say this is the signature of my function:
def foo(x,y,z=0):
.
.
When I want to use this function, how can I override the value of z without changing the function or the signature?
Just pass value explicitly.
def foo(x, y, z=0):
print z
foo(1,3)
>> 0 # default value
foo(1,2,5)
>> 5 # new value passed

Categories