This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 1 year ago.
In python want to do something like this:
def func(*args, flag):
do something
if cond A:
flag="some A"
else:
flag="some B"
Then I found the flag passed is always unchanged.
If I change the flag to be a list, then yes it works but is ugly.
What is the proper way to achieve the above?
Related
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 2 years ago.
Trying to understand why this is happening.
When I put tall() inside print(), it prints tall() but ALSO a None value. Why is the value None given after it has already printed out tall()?
def tall():
print('31337')
print(tall())
Result:
31337
None
You must know that a function always return a value and in your case your function is not returning any value so the None value also.
This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Closed 4 years ago.
How can I express a method (e.g. dir(os)) like a keyword (e.g. dir_ os)? Is it even possible?
The opposite is quite easy to achieve:
# `assert` expressed like a method
def assert_(x): assert x
assert_(1 == 1)
No, it's not possible. Parentheses are required in order to call a function or method.
This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Use a string to call function in Python [duplicate]
(3 answers)
Closed 4 years ago.
I have this simple function:
def fu():
return "great"
I need to call it by using a string,
So I tried this:
print(exec("fu()"))
But the the output I got was:
None
How do i fix it?
As in comments says you can not use exec for this purpose.
but eval will do what you want, full doc here:
>> eval('fu()')
"great"
Note that using eval is not the best practice.
There is a better way to access this function with globals or locals based on where you define your function, and I think it's better to use this instead of eval:
>> globals()['fu']()
"great"
This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 8 years ago.
Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__ returns the name, but I need it inside an own method.
How can get_name() be implemented?
This is not a duplicate of questions which answer is __name__
This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
I receive a string, for example "save". And i have a method save with paramethers.
How can i convert the string save in a call to save().
I tried with eval and exec.
Edit: Solved here --> Calling a function of a module from a string with the function's name in Python
def some_method(self):
save_method = getattr(self, 'save')
save_method() # save()