0

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

6
  • I get "AttributeError: 'str' object has no attribute 'functionname'", which makes more sense to me. I think your example is incomplete. Commented Aug 18, 2010 at 21:19
  • 1
    The first argument of getattr should be an object Commented Aug 18, 2010 at 21:20
  • @Rick, a module of that name or an object of that name? Commented Aug 18, 2010 at 21:24
  • 2
    @Rick, no need for us to make any modules or anything: getattr(x, fn) where x is anything at all (that doesn't produce a SyntaxError;-) and fn is a string, will not produce the error you report. So, we know that's not what you're doing. It remains for you to show us what you are doing, in a version as simplified as you can make it and still reproduce that exact error message -- if fn is the second argument, I can tell you confidently that it cannot possibly be a string, but I can't guess how exactly you bungled things w/o seeing some code of yours;-). Commented Aug 18, 2010 at 21:26
  • 2
    @Rick, those imports are totally irrelevant: the two lines that assign fn and call getattr, if right next to each other, cannot possibly produce the error you say they produce. Commented Aug 18, 2010 at 21:28

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks.. yeah you are right, I was confused about the usage, I found getattr in a post somewhere saying to do it this way to call a variablized function name but apparently it was not correct
@Rick, I don't get it, functionname is a module, and you call it with a parameter?
0

The first argument of getattr is the object that has the attribute you are interested in. In this case you are trying to get an attribute of the function, I assume. So the first argument should be the function. Not a string containing the function name, but the function itself.

If you want to use a string for that, you will need to use something like locals()[fn] to find the actual function object with that name.

Second, you're passing the function name to getattr twice. The function doesn't have itself as an attribute. Did you mean the second argument to be x? I don't really get what you're trying to do here, I guess.

1 Comment

its from an import so thats why its like that, I updated the OP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.