Back
Lesson 8:
Visibility
Introduction to function visibility in Vyper
Progress: 0%
Visit desktop version for better experiences.
Visibility
Vyper have three types of decorator to declare a function's visibility.
- @internal (default): can only be invoked within iself. The naming convention is to use
_
prefix. - @external: allows external calls into this contract
- @deploy: invoked once during contract deployment
# pragma version 0.4.0
# Using mapping example to demonstrate visibility decorators
mapping_example: HashMap[uint256, String[100]]
@deploy
def __init__():
self.mapping_example[0] = "Deploy Decorator"
# This is an internal function that is invoked only when `setMappingPairOneAndTwo` is called
def _setMappingPairOne():
self.mapping_example[1] = "Internal Decorator"
# Example below maps key value "1" to "Hello, World!"
@external
def setMappingPairOneAndTwo():
self._setMappingPairOne()
self.mapping_example[2] = "External Decorator"
# Only mapping_example[0] will have a value after contract deployment
# mapping_example[1] and mapping_example[2] will only have values after calling `setMappingPairOneAndTwo`
@external
@view
def viewMappingValue(x: uint256) -> String[100]:
return self.mapping_example[x]
Vyper Differentiators
Do not need to explicitly declare
@internal
decorator as it is the default.You will need to use
@deploy
decorator together with__init__()
function to invoke the constructor. This is equivalent to Solidity'sconstructor
keyword.