p.variables() returns a tuple which contains the variables of p, so your function could be written:
def f(q):