sage: cmds = "def somef():\n    return Integer(443r)\nres = somef()"

In your second attempt, both return 443 and res=somef() are part of the definition of somef, so that res is not defined.