I cannot figure out how to assign a variable based on the output of a function. I basically want to do this in my __init__ method

def square(x):
    y = x * x
    return y

toSquare = 10
result = square(toSquare)

My init method looks like this

def __init__(self):
            hash = sp.bytes('0x'),
            nat_value = self.somefunction(),   <-this does not work


Smart contracts are codeless during origination. By this, I mean that you don't run any smart-contract code during origination.

Above, you are leveraging meta-programming to build the initial storage of the contract, and you cannot use any entry point function there.

Example of what you can do:

import smartpy as sp

class MyContract(sp.Contract):
    def __init__(self):
        self.init(b = self.a())

    def a(self):         
        b = 1
        return b

    @sp.entry_point        <- Only executable at runtime
    def ep(self):

@sp.add_test(name = "Sample")
def test():
   scenario = sp.test_scenario()
   c1 = MyContract()
   scenario += c1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.