Python Forum
Possible to dynamically pass arguments to a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possible to dynamically pass arguments to a function?
#1
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.

attackers = board.attackers(chess.BLACK, chess.A7)
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...

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?
Reply
#2
Nevermind, I figured it out.

color = [chess.BLACK, chess.WHITE]
square = [chess.A7, chess.C7]

a = 1

attackers = board.attackers(color[a], square[a])
If anybody else ever wants to do this, here's how I did it. It will accept a slice or an integer, but not an immutable object like a string, which was the problem I was having.

Thanks everyone.
Reply
#3
You can look up attributes by name.
class ChessThing():
    """Standing for chess which I do not have"""
    def __init__(self):
        self.BLACK = 100
        self.WHITE = 200
        self.A0 = 0
        self.A1 = 1
        self.A2 = 2
        self.A3 = 3
        self.A4 = 4
        self.A5 = 5
        self.A6 = 6
        self.A7 = 7

chess = ChessThing()

# Get attributes from chess using the attribute name
colors = [getattr(chess, color) for color in ['BLACK', 'WHITE']]
squares = [getattr(chess, f'{r}{c}') for r in ['A'] for c in range(8)]

print(colors)
print(squares)
Output:
[100, 200] [0, 1, 2, 3, 4, 5, 6, 7]
Before doing that I would exhaust all efforts to see if there is any kind of collection for the A0..H7. But if you have to, you can get all 64:
squares = [getattr(chess, f'{r}{c}') for r in ['ABCDEFGH'] for c in range(8)]
grimm1111 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 800 Jul-27-2023, 12:40 AM
Last Post: tester_V
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,482 Sep-06-2021, 03:43 PM
Last Post: muzikman
  'namespace' shorthand for function arguments? shadowphile 5 2,541 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Why Pass Functions as arguments? muzikman 14 5,525 Jan-18-2021, 12:08 PM
Last Post: Serafim
  Function won't apply dynamically in timeseries illmattic 1 1,708 Jan-08-2021, 03:15 PM
Last Post: stullis
  how to pass arguments between pythons scripts? electricDesire 2 2,109 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Do I have to pass 85 variables to function? Milfredo 10 4,185 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020