-1

I've tried:

import a
from a import b
for i in range(1,10):
    c = position(i,2,3)
    maybe = getattr(b, str(c))
    position = geo[maybe]

After running this, I get the error in the title. But when I just write:

import a
from a import b
position = geo[b.position(1,2,3)]

I don't get an error. I've also tried using a function, but I get the same error:

def some_function(obj):
    for attribute in [c]:
        getattr(obj, str(attribute))
position = geo[some_function(b)]

How can I fix this so that I can use this variable as an attribute?

John
  • 1
  • 1
  • 1
    `getattr(b, 'position(1, 2, 3)')` != `getattr(b, 'position')(1, 2, 3)` – 0x5453 Aug 23 '22 at 16:10
  • @0x5453 is 'b.position(1,2,3)' = 'getattr(b, 'position')(1, 2, 3)'? – John Aug 23 '22 at 16:22
  • @PranavHosangadi how can I get `getattr(b, position(1,2,3))` using c? – John Aug 23 '22 at 16:31
  • @Mat apologies, I misspoke. `b.position(1, 2, 3)` is the same as `getattr(b, 'position')(1, 2, 3)`. I don't know what your first code does, because I don't know what `position` or `c` is. Please post a [mre] – Pranav Hosangadi Aug 23 '22 at 16:36
  • @PranavHosangadi How can I use `getattr(b, 'position')(1,2,3)` with a variable as (1,2,3)? Whenever I try to use a variable instead of (1,2,3) I get the error: `position.__init__(position, str) did not match C++ signature: __init__(_object*, int, unsigned int, unsigned char) __init__(_object*, int, unsigned int) __init__(_object*)` – John Aug 23 '22 at 17:17
  • _"Whenever I try to use a variable instead of (1,2,3)"_: What is the code you tried that gave you this error? – Pranav Hosangadi Aug 23 '22 at 17:25
  • @PranavHosangadi `for i in range(1,10): q = (i,2,3) maybe = getattr(b, 'position')(str(q))` If I remove `str` I get the same error except it says `position.__init__(position, tuple) did not match C++ signature: ...` – John Aug 23 '22 at 17:28
  • Well, your `b.position` method expects _three arguments_. You are passing it _one argument_ that is a string. Why not just do `maybe = getattr(b, 'position')(i, 2, 3)`? If you really want to use `q`, then [_unpack_ the tuple](https://stackoverflow.com/questions/1993727/expanding-tuples-into-arguments) into the arguments of the function using `maybe = getattr(b, 'position')(*q)` – Pranav Hosangadi Aug 24 '22 at 14:19

1 Answers1

0

You are not using the variable c in the correct way as @0x5453 said. use this instead:

import a
from a import b
for i in range(1,10):
    position = geo[b.position(i,2,3)]
M.Sarabi
  • 51
  • 1
  • 9