You can declare a as a global variable:
def test2():

Remember that SageMath now is based on Python 3, so you should write print(a) if you use SageMath 9.0.