Search code examples
ethereumsolidityweb3py

Why do I need parenthesis to access a Solidity contract variable in python?


inside a Solidity contract there is a declaration as follows:

contract Lottery is VRFConsumerBase, Ownable {
address payable[] public players;
...
...

elsewhere in the same contract,there is a an assignment as follows:

....
....
recentWinner = players[someIndex];
....
....

} 

the deploy.py Python script is used to deploy the contract. the function deploying the contract is:

def deploy_lottery():
      Lottery.deploy(....)
....
....

After deployment and doing other stuff... the contract's recentWinner variable is accessed from python script using parenthesis as follows:

def end_lottery():
      print(f"{lottery.recentWinner()} is the winner!")
  

My rather basic question is, why are parenthesis used? recentWinner is not a function that was defined in the Lottery contract. If I ran the script without the paranthesis, I get the following output.

<ContractCall 'recentWinner()'> is the winner!
Terminating local RPC client...

so it seems like the paranthesis is necessary. can somebody explain what is going on please? why should I treat this variable like a function in order to retrieve its value? if you can also point me to some related posts/reading material that would be much appreciated. thank you!


Solution

  • EVM does not have public variables, only public accessor functions.

    Behind the scenes, the Solidity compiler generates a function called recentWinner() for you. This is called accessor or getter function. Unlike in Java etc. languages there is no get prefix for the function.