Variable problems in PostgreSQL with language plpython3u I am setting up a function where recursive action is relevant. The function basically maps two floats  according to a rule and then returns a string. The code below is stripped for the actual function, but it should serve as a relevant code example (full code available). The original code (python 2.7) works fine, so I am thinking that there is something about the language, plpython3u, used in PostgreSQL which messes it up.
I imagine this is related to local variables and global variables. I would have thought the variables remained local, but it does not look that way. Is there a way to work around this?
Running the function I get an error message indicating that a variable has already been
ERROR:  UnboundLocalError: local variable 'square' referenced before assignment
CONTEXT:  Traceback (most recent call last):
  PL/Python function &quot;qdgc_get_recursivestring&quot;, line 3, in &lt;module&gt;
    square ='ABC'+square
PL/Python function &quot;qdgc_get_recursivestring&quot;
SQL state: 38000

Function:
CREATE OR REPLACE FUNCTION public.qdgc_get_recursivestring(
    lon_value double precision,
    lat_value double precision,
    depthlevel integer,
    square text)
    RETURNS text
    LANGUAGE 'plpython3u'
    COST 100
    VOLATILE PARALLEL UNSAFE

AS $BODY$

square ='ABC'+square

depthlevel = depthlevel -1

if depthlevel&lt;1:
    return square
else:
    return qdgc_get_recursivestring(lon_value,lat_value,depthlevel,square)
  
$BODY$;
It is necessary to declare a variable within the function. Add &quot;global square&quot; here:
[..]
AS $BODY$

global square

square ='ABC'+square
[..]

You will have to do the same for depthlevel.