Let's analyze the difference between using self.double() (with parentheses) and self.double (without parentheses) in your code, and why this affects the value at the end (specifically the hash output).
Code recap:
class MyClass():
def __init__(self, num):
self.num = num
def double(self):
return 2 * self.num
def my_fn(self):
# do not forget `()`; otherwise, not only will it be wrong, but it will change its value at different runs
text = f'Ana are {self.double()} mere.'
print(get_sha256(text))
obj = MyClass(8)
obj.my_fn() # 99264de5587efd3c0900f7752d6ec8fab661662c4a71efd30b49b6d181eb5134
Key point:
self.double() calls the method and returns the integer result: 2 * self.num.
self.double refers to the method itself as an object (a function/method), without calling it.
What happens if you omit ()?
If you write:
text = f'Ana are {self.double} mere.'
Then self.double is not called, so Python converts the method object into a string inside the f-string. The string representation of a method object looks like this:
<bound method MyClass.double of <__main__.MyClass object at 0x7f8a4c855460>>
Notice the memory address part (0x7f8a4c855460), which is dynamically assigned by Python every time you run the program (or can vary for different runs).
This means the string text becomes something like:
"Ana are <bound method MyClass.double of <__main__.MyClass object at 0x7f8a4c855460>> mere."
Why does the value (hash) change?
Because the string now contains this memory address that changes with every run of the program, the SHA256 hash computed with get_sha256(text) will differ each time you execute the program.
What is the correct usage?
You want to call the method to get the doubled number and insert that into the string:
text = f'Ana are {self.double()} mere.'
Now self.double() returns an integer (16 if self.num is 8), and your string becomes:
"Ana are 16 mere."
This string is the same each run (assuming self.num doesn't change), so the SHA256 value is stable.
Summary:
- With parentheses
(): method is called, results in a value that is stable and predictable.
- Without parentheses: method object itself is inserted, which converts to a string containing memory addresses and changes every run.
Bonus - showing difference explicitly:
print(self.double) # <bound method MyClass.double of <__main__.MyClass object at 0x7f8a4c855460>>
print(self.double()) # 16
Remember: Always add parentheses () when you want to call a method and use its result. When you omit them, you get the method object itself — not the result.