1/5
## Visibility in Solidity In this lesson, we will explore the concept of visibility within Solidity contracts. Visibility is a fundamental aspect of how we control access to different components within a contract. We will focus on the keywords `public` and `internal`. The concept of visibility allows us to define the scope of accessibility to different elements in our smart contracts. Let's dive in by examining the code for our `favorites.vy` contract: ```python #pragma version 0.4.0 @license MIT my_favorite_number: public(uint256) = 0 @external def store(new_number: uint256): self.my_favorite_number = new_number ``` We have a state variable `my_favorite_number` that is declared as `public`. This means it is visible and accessible from anywhere. For example, an external user could interact with the `my_favorite_number` variable directly, without having to use any functions. We also have a function `store` that is declared as `@external`. This means that this function can only be accessed from outside the contract, via a transaction. No other functions within the contract can call `store` directly. Now let's look at how `internal` visibility functions work. We can remove the `public` keyword from our `my_favorite_number` state variable. We can then change the `@external` decorator to `@internal`: ```python #pragma version 0.4.0 @license MIT my_favorite_number: uint256 = 0 @internal def store(new_number: uint256): self.my_favorite_number = new_number ``` We can then compile the code: ```bash Compile favorites.vy ``` The `internal` keyword means that the function `store` can only be called by other functions within the same contract. Let's add an example: ```python #pragma version 0.4.0 @license MIT my_favorite_number: uint256 = 0 @internal def store(new_number: uint256): self.my_favorite_number = new_number def store_other(): self.store(7) ``` When we try to compile this code, we get an error because the `store_other` function is trying to call the `store` function, which is declared as `internal`. We can fix this by making the `store` function `external`, which allows it to be called from outside the contract. We can also make `my_favorite_number` `public` again so that it's visible from the outside. ```python #pragma version 0.4.0 @license MIT my_favorite_number: public(uint256) = 0 @external def store(new_number: uint256): self.my_favorite_number = new_number def store_other(): self.store(7) ``` We can compile this code and we will see it functions as expected. This is a basic introduction to the concept of visibility in Solidity contracts. By mastering the use of keywords like `public` and `internal`, we can create more secure and modular smart contracts.
A comprehensive guide to understanding function visibility in Vyper. The lesson covers the concepts of internal, external, and public visibility decorators, explaining how they control access to functions and state variables. It also demonstrates the impact of visibility on function calls and contract deployment.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar