-1

here in func1(), i have some variables and there values. how can i call func1() in func2() to get all the variables and values as i want to pick rules from func2().(as we cannont use local variables in other functions) Also there is func3(), which needs func1() values. what should be the condition to get b (b>a) and pass back to func2() to pick only specific rules.

def func1():  
    a=1  
    b=2  
    c=3  
func1()
def func2():  
    if a==1:  
        print("rule=good")  
    elif b==2:  
        print("rule=poor")  
    elif c==3:  
        print("rule=very poor")  
func2()  
def func3():  
    if b>a: 

can we save func1() into a variable and pass the variable to func2() parameter? print(save) gives output=none

def func1():  
    a=1  
    b=2  
    c=3  
    save = func1()  
print(save)  
def func2(value):  
    if a==1:  
        print("rule=good")  
    elif b==2:  
        print("rule=poor")  
    elif c==3:  
        print("rule=very poor")  
func2(save)  
def func3():  
    if b>a: 

the actual output should be: func2 gives:

   rule=good 
   rule=poor 
   rule=very poor 

func3 gives only:

   rule=poor
mssd21
  • 25
  • 4
  • Can you please restate your problem? It is difficult to understand what you need to accomplish. Consider `return` values from functions, and [nested functions](https://stackoverflow.com/questions/40235074/defining-functions-inside-of-other-functions). – sal Apr 28 '19 at 16:11

2 Answers2

0

You don't seem to have understood how functions work yet. Anything you don't return (or yield) from a function will not be available outside your function.

To do what you want to achieve (if I understood you correctly), can do this:

  • Define the variables in func1
  • Pass the variables as a dict to func2 & func3
  • Define func2 & func3 in a way such that they unwrap the dict into the variables.
def func1():  
    return {'a':1, 'b':2, 'c':3}

def func2(a, b, c):  # You're taking the variables by named parameters here; a,b,c are argument parameters here - they only exist inside the function
    if a==1:  
        print("rule=good")  
    elif b==2:  
        print("rule=poor")  
    elif c==3:  
        print("rule=very poor")  

def func3(a, b, c):  
    if b>a: 
       print("something")

vars = func1() # Save the variables in `vars`
print(**vars)

func2(**vars)  # Unroll vars into `a=1,b=2,c=3`
func3(**vars)  # Same
rdas
  • 20,604
  • 6
  • 33
  • 46
0

First, let's define an approximation to your second function

def f2(a,b,c):
    if a==1: print('a equals 1')
    if b==2: print('b equals 2')
    if c==3: print('c equals 3')

next, your first function, please note that

  1. you need to return values from the first function,
  2. the three values you return are packed into a single object, i.e., a tuple

so our function will be

def f1():
    # compute compute
    return 1,2,3

To pass these values to the second function, you have to unpack the returned single object (a tuple) — to unpack, you just need to know that the unpacking operator is a single * placed in front of an iterable, hence your call of f2 will be written as

f2(*f1())

Sample Session

$ python
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f1():
...     # compute compute
...     return 1,2,3
... 
>>> def f2(a,b,c):
...     if a==1: print('a equals 1')
...     if b==2: print('b equals 2')
...     if c==3: print('c equals 3')
... 
>>> f2(*f1())
a equals 1
b equals 2
c equals 3
>>> ^D
$
gboffi
  • 22,939
  • 8
  • 54
  • 85