Reputation: 370
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
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
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