5/5
## Introduction to Vyper Modules We're going to be learning about Vyper modules. We'll create a new file called `five_more.vy`. It's going to be very similar to the `favorites.vy` contract, but we'll modify the `store()` function to add five to the number stored. ```python self.my_favorite_number = new_number + 5 ``` We want to reuse as much code as possible without copy-pasting everything, so we'll use Vyper's module system. A module is a way to encapsulate reusable code. Vyper favors **composition over inheritance** - we compose modules to create new contracts. Our `favorites_factory.vy` contract imports the `I_favorites` module: ```python from interfaces import I_favorites ``` The `I_favorites` module helps us type our list of `favorites` contracts. ```python list_of_favorite_contracts: public(DynArray[I_favorites, 100]) ``` When we import a module, it doesn't automatically become part of our contract. We need to explicitly tell Vyper how to use it. We can import the `favorites` contract into our `five_more.vy` file: ```python import favorites ``` Now, if we comment out the variables and functions that we imported, the contract still compiles because we only imported it, not copied it. ```python # my_favorite_number: uint256 # @external # def store(new_number: uint256): # self.my_favorite_number = new_number ``` We can verify this by running the following commands: ```bash mox compile ``` The ABI of our `five_more.vy` contract will be empty since we're just importing it. ```bash # mox run deploy ```
We're going to be learning about Vyper modules.
We'll create a new file called five_more.vy
. It's going to be very similar to the favorites.vy
contract, but we'll modify the store()
function to add five to the number stored.
We want to reuse as much code as possible without copy-pasting everything, so we'll use Vyper's module system.
A module is a way to encapsulate reusable code.
Vyper favors composition over inheritance - we compose modules to create new contracts.
Our favorites_factory.vy
contract imports the I_favorites
module:
The I_favorites
module helps us type our list of favorites
contracts.
When we import a module, it doesn't automatically become part of our contract. We need to explicitly tell Vyper how to use it.
We can import the favorites
contract into our five_more.vy
file:
Now, if we comment out the variables and functions that we imported, the contract still compiles because we only imported it, not copied it.
We can verify this by running the following commands:
The ABI of our five_more.vy
contract will be empty since we're just importing it.
A comprehensive guide to understanding and utilizing Vyper modules. The lesson delves into the concept of modules as a means of achieving code reuse in Vyper, contrasting it with inheritance.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using 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)
Last updated on August 11, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using 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)
Last updated on August 11, 2025