-1

Now I know there are many duplicates asking a similar question [1][2][3][4][5], however I believe none of them call a function with multiple input parameters inside another function with multiple parameters in the same Class; which happens to be my particular problem (unless I missed it during my search).

Below is a minimum working example where I am trying to use the results of function1 in function2:

class Something():
    def __init__(self,input1, input2, input3):
        self.input1 = input1
        self.input2 = input2
        self.input3 = input3

    def function1(self, input4, input5, input6):
        something = (self.input1 +  self.input2 + self.input3)+input4+input5+input6
        return something

    def function2(self, input7, input8):
        something2 = self.function1(self.input4, self.input5, self.input6)+input7+input8
        return something2        

a = Something(1,2,3)
print(a.function1(4,5,6))
print(a.function2(7, 8))

Function1 prints out fine, however when I call Function2, I get an error:

AttributeError: 'Something' object has no attribute 'input4'

I'm probably missing a simple step here, however I can't seem to figure out how to use the results of function1 inside function2 without having to redefining all the input parameters again.

4 Answers 4

1

input4 is an argument whose scope is local to function1. The same applies to input5 and input6. You need to assign them using self in the __init__ in order to make them accessible to the class methods using the self instance. If you want these variables to be accessible without redefining them in the argument list, you can assign them using self in the function1

class Something():
    def __init__(self,input1, input2, input3):
        self.input1 = input1
        self.input2 = input2
        self.input3 = input3

    def function1(self, input4, input5, input6):
        self.input4 = input4
        self.input5 = input5
        self.input6 = input6
        something = (self.input1 +  self.input2 + self.input3)+input4+input5+input6
        return something

    def function2(self, input7, input8):
        something2 = self.function1(self.input4, self.input5, self.input6)+input7+input8
        return something2        

a = Something(1,2,3)
print(a.function1(4,5,6))
print(a.function2(7, 8))

# 21 
# 36
Sign up to request clarification or add additional context in comments.

Comments

0

self.input4 makes no sense, because it's not a field that you give to self in __init__.

If you want to use self.input4 in function2, then either __init__ or function1 need to assign self.input4 = # something.

Comments

0

Just by adding another argument to function2, you can either pass the results of function1 to function2, or just call function1 in the args of function2.

class Something():
    def __init__(self,input1, input2, input3):
        self.input1 = input1
        self.input2 = input2
        self.input3 = input3

    def function1(self, input4, input5, input6):
        something = (self.input1 +  self.input2 + self.input3)+input4+input5+input6
        return something

    def function2(self, input7, input8, res1):
        something2 = res1+input7+input8
        return something2

a = Something(1,2,3)

# just save the result of function1 and pass it to function2
res1 = a.function1(4,5,6)
print(res1)
print(a.function2(7, 8, res1))

# or just call function1 in the arg list of function2
print(a.function2(7, 8, a.function1(4,5,6)))

Comments

0

"Scope" is a fancy term to denote the accessibility, and often lifetime of variables. input4 cannot be seen inside function2 because it's only defined by the name input4 within function1. If you want to use it in another function, you should pass it as a parameter or save it as state.

Your second function doesn't actually depend directly on the internal state of your object, so you can just split it out as a function separate from your class. function1 should have a meaning and pass that meaning into your other function.

# A separately reusable function, not dependent on Something
def function2(foo, input7, input8):
    something2 = foo + input7 + input8
    return something2        

class Something():
    def __init__(self,input1, input2, input3):
        self.input1 = input1
        self.input2 = input2
        self.input3 = input3

    def function1(self, input4, input5, input6):
        something = (self.input1 +  self.input2 + self.input3)+input4+input5+input6
        return something

a = Something(1,2,3)
print(a.function1(4,5,6))
print(function2(a.function1(4,5,6), 8))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.