AnthonyM
AnthonyM

Reputation: 33

python is "self" required on variable in function?

lets say I have a class myArithmetics and two functions within the class. The first function is called find_sum() and the second is is_max()

class myAirthmetics

    def __init__(self):
        pass
    
    def find_sum(self):
        numb = 15
        self.sum = 15 + numb

    def is_max(self):
        if self.sum > 20:
            print("yes")
        else:
            print("no")

My question is: Does the variable numb need to be self.numb? Since numb is not being accessed/used outside of the function find_sum(), it should not need to have self. ?

The only variables that require self are variables that are being accessed outside of their function?

Upvotes: 0

Views: 46

Answers (1)

Adin Ackerman
Adin Ackerman

Reputation: 312

You are correct, if you only want to use that variable in the scope of that function, there is no need for self.

P.S. I advise against naming a variable sum as that is a built-in function.

Upvotes: 3

Related Questions