calling functions python from outside - python

I have small a python script looking something like this:
def number1():
x = 1
open_numbers = []
open_numbers.append(x)
return open_numbers
def myfunction(open_numbers):
y = 1
open_numbers.append(y)
I would like to call the myfunction in the the end of the script. Using
myfunction()
But it keeps telling me missing 1 required positional argument: 'open_numbers'
Tried passing the argument and got name 'open_numbers' is not defined
I plan to add more functions later and run them the same way
function(arg)
function2(arg)
function3(arg)

Solution
First of all, your code was not properly indented. I have corrected that.
The function myfunction takes in a list (open_numbers) as input and should return it as well.
I have passed in the output of number1() as the input to myfunction(). This should create a list: [1, 1]. And that's what it did.
def number1():
x = 1
open_numbers = []
open_numbers.append(x)
return open_numbers
def myfunction(open_numbers):
y = 1
open_numbers.append(y)
return open_numbers
myfunction(number1())
Output:
[1, 1]

you need to pass in an object to your function. you can call your function with an empty list if you want:
a = []
myfunction(a)

You might want to define default parameter, and return the updated value
def myfunction(open_numbers = []):
y = 1
open_numbers.append(y)
return open_numbers
Then you can call it with passing parameter
myfunction([1]) or without myfunction()

Related

Getting outputs together into a dictionary

def firstone():
x= 1
print(x)
def firsttwo():
y=1
print(y)
**D={firstone,firsttwo}** ***#Problem is here***
What can be done to run the last line properly? Any ideas?
You should return a value from your functions, if you just print(), their return would be None. If your goal is to create a dict when the output of firstone() is the key and the output of firsttwo() is the value:
Code:
def firstone():
return 1
def firsttwo():
return 1
d = {firstone(): firsttwo()}
Output:
{1: 1}
First you need to make sure your functions actually return a value. Change it from printing to returning. Then call those function in the dictionary (notice the parentheses around the function name?).
def firstone():
x= 1
return x
def firsttwo():
y=1
return y
d = {"firstone":firstone(),"firsttwo":firsttwo()}

Python function argument can't be used as a variable in a child function

Here's the problem I try to solve:
I have a first function, to which I put in arguments. Then, later on, I have a second function, from which I want to call, as a variable, the said argument of the parent function. So it goes like:
def parent_function(argument=x):
if statement:
child_function()
else:
...
return result
def child_function():
x = x + 5
return x
If I run such a code, I get an error in the child function saying name 'x' is not defined.
However, if I fix my code to make x global in the parent function, like this:
def parent_function(argument=x):
global x
if statement:
child_function()
else:
...
return result
def child_function():
x = x + 5
return x
I get the error name 'x' is parameter and global
I need to import both functions in another file and I can't "dismantle" the child function inside the parent function.
Thanks very much for any help !
Don't use global Variables. Every function needs it's own arguments:
def parent_function(x):
if statement:
x = child_function(x)
else:
...
return result
def child_function(x):
x = x + 5
return x
name 'x' is parameter and global means you can't overwrite parameter x for being global also. To fix this, use another variable y, like this:
def parent_function(argument=x):
global y
y = x
if statement:
child_function()
else:
...
return result
def child_function():
y = y + 5
return y
This error happens because you are trying to overwrite a parameter in a function whose scope is local to that function by giving it a global scope. The problem is that variables defined in the context of a function are by definition local variables. To better illustrate this problem ,you can simply try to launch this piece of code:
def parent_function(argument="Hello"):
global argument
return argument
You will see that it will fail to run for the same reason that I have explained. I hope I have been clear in my explanation. Good luck.
The first thing you need to change is this:
def parent_function(argument=x):
If you search for how to make a default argument in a function you will get something like this: https://www.geeksforgeeks.org/default-arguments-in-python/
. This means instead of x you need to have some default value, for example:
def parent_function(argument=5):
This means that if you do not pass the argument called argument to the function value 5 will be passed.
On the other hand, it seems that you want x to be an argument, which means the def line should look like this:
def parent_function(x=5):
Second, global keyword needs to be used in the child_function since x has not been used in parent_function. This leads to this:
def parent_function(x=5):
if statement:
child_function()
else:
...
return result
def child_function():
global x
x = x + 5
return x
To have all this work, there must be at least two more lines one to set x and another to call parent_function, like this:
x = 6
parent_function(4)
But, to be even funnier, x from the arguments in parent_function and x used in child_function are not the same thing, and you can see for yourself in this example which is similar to your code, but fully executable:
def parent_function(x=5):
if True:
print(child_function())
else:
print("else branch")
return True
def child_function():
global x
x = x + 5
return x
x = 6
parent_function(4)
This prints out 11 even you might think it will print out 9!
This is due to fact that keyword global refers to the (as the word says) global variable declared outside of the functions, the variable with value 6. Usually, local and global variables should have different names, so either the argument x in parent_function or global x variable needs to be renamed.
IDK if this helps, but you will learn something from this, for sure!

How to pass function as an argument to another function, without running it automatically

I have 2 functions - one that is checking object status and the second one that is creating some connections when allowed. I'm trying to pass a function as an argument, but return that only when conditions will be correct. The issue is it is running anyway.
testList = []
def testObject(name, funct):
if name == "testName":
b = funct
return name + str(b)
else:
return None
def someFunction(vector, opt, axis):
testList.append("Something")
return opt * axis
a = testObject("testName2", someFunction([0,0,0], 1, 6) )
print a
print testList
Output:
print a
print testList
None
['Something']
So even if "A" is current, testList should be empty.
Wonder what I'm doing wrong.
The problem is that you are calling your function when you're trying to pass it as a parameter. Instead of passing it as someFunction(...), pass it as someFunction.
e.g.
a = testObject("testName2", someFunction )
instead of what you have. This will pass the function 'object', ready to be called. Then, inside the other function, simply do:
def testObject(name, funct):
if name == "testName":
b = funct([0,0,0], 1, 6)
I assume you'd want to pass the values as different arguments, as I assume these aren't constants, but this should be enough to guide you.
If you want to pass it in with arguments already set, I'd definitely give a look into functools.partial, but keep in mind that it might become a bit messy.
For partial:
from functools import partial
partial_function = partial(someFunction, [0,0,0], 1, 6)
a = testObject("testName2", partial_function )
------
def testObject(name, funct):
if name == "testName":
b = funct()

What is the syntax for the input for a def function with multiple nested functions?

I'm learning Python right now and I am just trying to get to grips with all of the syntax options.
Currently, the only thing that I can't seem to google up is what to do if I for some reason want to define a function which contains multiple other defines.
While I understand what to do if there's only 1 define inside the the larger define (val = f()(3,4) returns 7 if you exclude the second def below), I don't know how to correctly use the function below.
If it's possible, what is the syntax for a def function with an arbitrary amount of defined functions within it?
Code:
def f():
def x(a,b):
return a + b
return x
def y(c,d):
return c + d
return y
val = f()(3,4)(5,6)
print(val)
I expected the above to return either (7,11) or 11. However, it returns 'int object is not callable'
When you write val = f()(3,4)(5,6), you want f to return a function that also returns a function; compare with the simpler multi-line call:
t1 = f()
t2 = t1(3,4)
val = t2(5,6)
The function f defines and returns also has to define and return a function that can be called with 2 arguments. So, as #jonrsharpe said, you need more nesting:
def f():
def x(a, b):
def y(c, d):
return c + d
return y
return x
Now, f() produces the function named x, and f()(3,4) produces the function named y (ignoring its arguments 3 and 4 in the process), and f()(3,4)(5,6) evaluates (ultimately) to 5 + 6.

List of objects function not working

Sorry for the title, I hope it reflects correctly my problem :
In the following code, I was expecting the result to be result 0 1 2 but instead I have 2 2 2. The code inside my_function seems to be interpreted with the last instance of obj. What is wrong ?
class Example:
def __init__(self, x):
self.x = x
def get(self):
return self.x
a_list = []
for index in range(3):
obj = Example(index)
def my_function(x):
#some stuff with x like obj.another_function(x)
return obj.get()
a_list.append(my_function)
for c in a_list:
print(c())
When you define this
def my_function():
return obj.get()
Python will understand that my_function should run the get() method of an object called obj and return the value. It won't know the value of obj and what the get() method does until you attempt to call it.
So, you are actually defining three different functions that will eventually do the same thing. And, in the end, running the same code thrice.
But why is the return 2 2 2?
Because after the last iteration, the value of obj is Example(2)* because you redefine its value at every iteration, and the last one remains.
*
because of this line obj = Example(index)
Understanding a few things about how python works will help you understand what's happening here. Here obj is a closure, closures are evaluated at call time, not when the function is defined so if I do this:
x = "hello"
def printX():
print x
x = "goodbye"
printX() # goodbye
I get "goodbye" because printX is referencing a global variable in my module, which changes after I create printX.
What you want to do is create a function with a closure that references a specific object. The functional way to do this is to create a function that returns another function:
x = "hello"
def makePrintX(a):
def printX():
# We print a, the object passed to `makePrintX`
print a
return printX
# x is evaluated here when it is still "hello"
myPrintX = makePrintX(x)
x = "goodbye"
myPrintX() # "hello"
If you're having trouble understanding the above example I would recommend reading up on python's scoping rules. For your example, you could do something like this:
class Example:
def __init__(self, x):
self.x = x
def get(self):
return self.x
def makeObjFunction(obj):
def objFunction(x):
return obj.get()
return objFunction
a_list = []
for index in range(3):
obj = Example(index)
my_function = makeObjFunction(obj)
a_list.append(my_function)
for c in a_list:
print(c("some value"))
You are appending three my_functions to the a_list which are all closures over the same Example object. Try:
def my_function():
return obj
<__main__.Example object at 0x0054EDF0>
<__main__.Example object at 0x0054EDF0>
<__main__.Example object at 0x0054EDF0>
You can see they have the same id so calling get() on each should give the same answer.
If you just append the obj.get function (and drop the my_function) it'll work fine.
a_list.append(obj.get)
....
0
1
2
Edit: You've updated your question so to let you do more stuff in my_function(). It's still basically a scoping problem.
def my_func_factory(p_obj):
def my_function(x):
#some stuff with x like obj.another_function(x)
return p_obj.get()
return my_function
for index in range(3):
obj = Example(index)
a_list.append(my_func_factory(obj))
Since my_function can't see obj being reassigned, each instance doesn't pick up the change.
I think append() during the for just append the function address in a_list[]. After for iteration, the a_list is really given the number. Then it discovers the address of my_function, and they get the number in my_function, this is, 2. That's why you get [2,2,2].
Or maybe, in my_function, function give the method of "obj". But for iteration change the "obj" memory address each time, so the symbol "obj" always aim to the newest object Example. Due to my_function always get "obj", you get the same number from the last object.

Categories

Resources