Carlitos_30
Carlitos_30

Reputation: 370

Class method don't update attribute

I have this code:

class Test:
    def __init__(self, a):
        self.a = a
        self.success = True
    def method(self):
        self.success = False

test = Test("tree")
test.method
print(test.success)   

#output is: True

I need to check if the operation programmed in "method" is successful. So, on success, I update the "success" attribute declared in the constructor. But when I call the method after creating the class object, the attribute is not updated.

Upvotes: 0

Views: 262

Answers (2)

Dougie
Dougie

Reputation: 485

You're not calling your method() correctly.

class Test:
    def __init__(self, a):
        self.a = a
        self.success = True
    def method(self):
        self.success = False

test = Test("tree")
test.method()  # NOTE the parentheses here
print(test.success)   

Upvotes: 1

P.Gupta
P.Gupta

Reputation: 585

To invoke a method,you have to use parenthesis. In short,

test.method() is the correct way to call the method.

Upvotes: 2

Related Questions