I'm trying to do the following:
import sys; sys.path.append('/var/www/python/includes')
import functionname
x = 'testarg'
fn = "functionname"
func = getattr(fn, fn)
func (x)
but am getting an error:
"TypeError: getattr(): attribute name must be string"
I have tried this before calling getattr but it still doesn't work:
str(fn)
I don't understand why this is happening, any advice is appreciated
解决方案
It sounds like you might be wanting locals() instead of getattr()...
x = 'testarg'
fn = "functionname"
func = locals()[fn]
func (x)
You should be using getattr when you have an object and you want to get an attribute of that object, not a variable from the local namespace.