5/5
## Working with Comments and Directives in Vyper When writing code in any programming language, including Vyper for smart contracts, it's crucial to make your code understandable, both for others and for your future self. Comments are a fundamental tool for achieving this clarity. This lesson explores how comments work in Vyper, including their syntax, purpose, and a critical exception involving compiler directives. ### What are Comments in Vyper? In Vyper, comments allow you to add explanatory notes, reminders, or annotations directly within your source code. These notes are intended for human readers and are typically ignored by the Vyper compiler when it translates your code into executable bytecode. The syntax for comments in Vyper is straightforward and will be familiar to Python developers: you use the hash symbol (`#`). Everything on a line that follows the `#` symbol is considered a comment. ```vyper # This entire line is a comment. some_variable: uint256 = 100 # This part after the code is also a comment. ``` You can use comments to explain complex logic, document the purpose of variables or functions, or leave reminders for future tasks. ```vyper # TODO: Refactor this function for better gas efficiency # This variable stores the total supply of the token total_supply: uint256 = 1000000 ``` ### The Exception: Compiler Directives (`pragma`) While the rule is that comments starting with `#` are ignored by the compiler, there's a vital exception: **compiler directives**. These are special lines that, although they start with `#`, provide instructions *to* the compiler itself. The most common and essential compiler directive in Vyper is the `pragma version` directive. It tells the compiler which version of the Vyper language your contract is written for. ```vyper # pragma version 0.4.0 ``` Even though this line starts with `#`, the keywords `pragma` and `version` signal to the compiler that this is not just a simple comment to be ignored. Instead, the compiler reads this line to understand that the code should be compiled using a compiler compatible with version 0.4.0. This ensures that language features and syntax specific to that version are correctly interpreted. You can add regular comments above the `pragma` directive to explain its purpose, as those lines *will* be ignored: ```vyper # This is how we tell the vyper compiler # what version to use # pragma version 0.4.0 ``` In contrast, a line with random text after a hash, without specific keywords like `pragma`, remains a standard comment and is ignored: ```vyper # asdfaoglsa asdfasrtafdsdf <- This is ignored by the compiler. ``` ### Leveraging Comments and the Python Connection Because Vyper is heavily influenced by Python (often described as "Pythonic"), its comment syntax is identical. If you are learning Vyper, you are reinforcing or learning a core concept directly applicable to Python development. Use comments liberally as you write or follow along with tutorials. They are invaluable for leaving notes to yourself explaining *why* a piece of code is written a certain way or what it's intended to achieve. ### Practical Tip: Saving Your Code When working with web-based Integrated Development Environments (IDEs) like Remix (often used for Vyper and Solidity development), be mindful that your code might be lost if your browser cache is cleared, you accidentally close the tab, or the session expires. **Always back up your work locally.** A simple and effective method is: 1. Select all the code in the web IDE's editor. 2. Right-click and choose "Copy". 3. Paste the code into a text file on your local computer using a text editor (like VS Code, Sublime Text, Notepad, etc.) or a notes application. 4. Save the file frequently. This habit prevents potential data loss and ensures you always have a local copy of your Vyper contracts. In summary, use the `#` symbol for comments in Vyper to document your code, remembering the special `# pragma version` directive is essential for specifying the compiler version. Embrace comments as a tool for clarity and always save your work locally when using web-based tools.
An essential overview to Working with Comments and Directives in Vyper - Discover how comments (`#`) improve Vyper code clarity and grasp the vital compiler instructions provided by `# pragma` directives.
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)
Last updated on June 11, 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)
Last updated on June 11, 2025