DEV Community

Cover image for Vyper Beginner's tutorial: Syntax and Structure.
Muwawu Moses
Muwawu Moses

Posted on • Edited on

Vyper Beginner's tutorial: Syntax and Structure.

What is Vyper?

Vyper(vyperlang) is a python-like programming language for writing smart contracts on the Ethereum blockchain just like Solidity.
It is a statically-typed language, which means that the type of each variable must be explicitly declared. This helps to prevent type-related errors and makes the code easier to read and understand.

Vyper also has a number of features that are designed to improve the security of smart contracts. For example, Vyper does not allow for low-level operations, such as direct memory access, which can be a source of vulnerabilities in smart contracts. Vyper also has built-in support for security features, such as reentrancy guards and overflow protection.

This makes Vyper a popular choice among developers whose major emphasis is on security, readability and simplicity.

During the course of this tutorial, our major emphasis is going to be on the basic syntax and structure of a vyper contract.

Prerequisites:

  1. Basic Knowledge in Python or any other programming language.

  2. A well set up development environment(i recommend one of my previous articles for this purpose)

  3. And lastly, we shall need to interact with our smart contracts using web3.py( Again, i recommend my previous article for this purpose)

Well! With all the above in place, we are now good to go.

Syntax and Structure:

Just like any other programming language, vyper also has several specific syntax rules that you should be aware of. These rules help ensure code readability, security and simplicity I will advise you to visit the vyperlang official documentation for more information about the vyperlang syntax.

As you might have noticed, learning the syntax is one thing but also applying the knowledge is another. Therefore, let's use a simple smart contract example;

# A simple Vyper contract
@external
def sayHello() -> String[13]:
    return "Hello, World!"

Enter fullscreen mode Exit fullscreen mode
  1. I will start with the first line of code:
# A simple Vyper contract
Enter fullscreen mode Exit fullscreen mode

This is simply a line of comment in our vyper code. Such code is ignored by the compiler and therefore, it's always meant to provide explanation to human understanding of the code.

3.

   @external
Enter fullscreen mode Exit fullscreen mode

This is a decorator that specifies that the following function is part of the public interface of the smart contract. It means that this function can be called from outside the contract, such as by other contracts or users interacting with it.

Please note that all functions must include one visibility decorator (@external or @internal). The remaining decorators are optional.

4.

   def sayHello() -> String[13]:
Enter fullscreen mode Exit fullscreen mode
  • def is a keyword used to define a function.
  • sayHello is the name of the function. You can call this function sayHello to execute the code within it.
  • (): The parentheses are used to list parameters if the function takes any. In this case, sayHello does not take any parameters.
  • -> String[13]: This part specifies the return type of the function. It indicates that the function will return a string of a maximum length of 13 characters.

5.

   return "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

The function body contains the code that is executed when the function is called.

  • return is a keyword that specifies the value to be returned by the function.
  • "Hello, World!" is a string literal, and in this case, it is the value that the function returns. The length of this string is 13 characters, matching the specified return type String[13].

Conclusion

Vyper is a very secure and simple programming language meant for writing smart contracts targeting the Ethereum Virtual Machine(EVM). We meet in the next tutorial about variables If you found this post helpful, please like and share it to others. Any kind of interaction in the comment section will do me good and i am open to any kind of opinion. Thank you.

Top comments (0)