Python - Need help to understand how to call a function [duplicate] - python

This question already has answers here:
What does it mean to "call" a function in Python? [closed]
(4 answers)
Closed 5 years ago.
I am new to programming, and trying out with a little Python3.
I have some trouble understanding the concept behind calling a function? having the defined the following function, what would be the proper way to call it?
def string_length(mystring):
return len(mystring)
Thanks in advance guys

def string_length(mystring):
return len(mystring)
print(string_length('something'))
Like that

length = string_length('some_string')
where length is a variable that will store the output.

Related

Convert string to function name in python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I have many functions with the same prefix. I want to use a combination of strings to invoke the function.
def func_plus_one(v):
return v+1
def func_plus_two(v):
return v+2
a='plus_one'
b='plus_two'
So how can I use 'func_'+a and 'func_'+b to use the function?
If the functions are in the same module as the code needing to reference them, use the globals() of the module. You could call the function indicated by a using:
globals()['func_' + a](x)
If they are in another module, use getattr
getattr(some_module, func_' + a)(x)

Python Error - 'int' object is not callable [duplicate]

This question already has answers here:
int object not callable error in python [closed]
(2 answers)
Closed 2 years ago.
I have researched this error and know that it has something to do with the naming of my variables, but I don't see any variables in my code that are "magic words". What am I missing? Thanks!
def shrink_inv (p,r,n,t):
return p(1+r/n)**n*t
shrink_inv(10,-0.1,1,1)
return p(1+r/n)**n*t
You are missing an operator after p - it's currently interpreted as a function call.

Simply python script is giving error, what is wrong? [duplicate]

This question already has answers here:
How do I forward-declare a function to avoid `NameError`s for functions defined later?
(17 answers)
Closed 3 years ago.
Simple python script is giving error, what is wrong?
var ="first variable"
myfun(var)
def myfun(var):
print(var)
Error -> NameError: name 'myfun' is not defined
This thing is quite obvious though. Python reads the code line by line and not like C.
Just switch your two blocks i.e. definition of function and calling it.
var ="first variable"
def myfun(var):
print(var)
myfun(var)
This should be good to go.
When python interpreter sees the statement myfun(var), the name myfun is not defined yet. You need to move this line after your function definition.

what does "..." mean and difference between ... and pass in python? [duplicate]

This question already has answers here:
What does the Ellipsis object do?
(14 answers)
Closed 3 years ago.
Well...When I define a function that does not perform certain actions I don't understand what the difference between ... and pass. For example, the following two pieces of code:
def _clear() -> None: ...
def _clear() -> None: pass
They are the same, right? Either one can be used? I sincerely hope someone can help me thoroughly understand it. Thanks...
pass is the standard way to say that a function does not do anything.
Placing the ellipsis object does yield valid code just like def _clear() -> None: x = 5, but most IDEs and static analysis tools will complain that the statement has no effect.

Python constructor overload with different number of parameters [duplicate]

This question already has answers here:
How to overload __init__ method based on argument type?
(10 answers)
Python function overloading
(19 answers)
Closed 4 years ago.
I am trying to build a complex number calculator in python. The number will be represented by a class with two fields. The first constructor I want to use looks like this:
def __init__(self,real,imag):
self.real = real
self.imag = imag
but i would like to use another, where the only parameter will be a string representing the whole number:
def __init__(self,string_number):
How to implement the overload that allows to use different number of parameters? I am more used to C++, where such thing was easy.
Thanks a lot!
EDIT:
Question marked as duplicate: I did not find any answer regarding different number of parameters. The error I get when trying to build a new instance of the class is about to few arguments.

Categories

Resources