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

Rick
  • 16,612
  • 34
  • 110
  • 163
  • I get "AttributeError: 'str' object has no attribute 'functionname'", which makes more sense to me. I think your example is incomplete. – robert Aug 18 '10 at 21:19
  • 1
    The first argument of `getattr` should be an object – NullUserException Aug 18 '10 at 21:20
  • @Rick, a module of that name or an object of that name? – robert Aug 18 '10 at 21:24
  • ok the OP is updated to more explicit – Rick Aug 18 '10 at 21:26
  • 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;-). – Alex Martelli Aug 18 '10 at 21:26
  • 2
    @Rick, those `import`s 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. – Alex Martelli Aug 18 '10 at 21:28

2 Answers2

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.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 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 Aug 18 '10 at 21:28
  • 1
    @Rick, I don't get it, `functionname` is a module, and you _call_ it with a parameter? – John La Rooy Aug 18 '10 at 21:33
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.

kindall
  • 178,883
  • 35
  • 278
  • 309