Feb-21-2021, 05:26 AM
Hello,
Probably a newbie question here.... but is there any way to dynamically pass arguments to a function? I am accessing a class from someone else's code, and I can't modify it.
The first argument can either be (chess.BLACK) or (chess.WHITE), and the second can be anything from (chess.A1) to (chess.H8)... so a total of 128 combinations.
I would like my program to be able to loop through each possible argument for this method, but I don't want to hardcode 128 lines of code in to my program.
If I try to do something intuitive like this, it doesn't work...
Can an attribute be a variable? How would I handle this?
Probably a newbie question here.... but is there any way to dynamically pass arguments to a function? I am accessing a class from someone else's code, and I can't modify it.
1 |
attackers = board.attackers(chess.BLACK, chess.A7) |
I would like my program to be able to loop through each possible argument for this method, but I don't want to hardcode 128 lines of code in to my program.
If I try to do something intuitive like this, it doesn't work...
1 2 3 4 |
color = 'BLACK' square = 'A7' attackers = board.attackers(chess.color, chess.square) |
Error:AttributeError: module 'chess' has no attribute 'color'
Because it's trying to read the attribute literally, rather than treating it as a variable. Can an attribute be a variable? How would I handle this?