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


Messages In This Thread
Possible to dynamically pass arguments to a function? - by grimm1111 - Feb-21-2021, 05:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 893 Jul-27-2023, 12:40 AM
Last Post: tester_V
  calling external function with arguments Wimpy_Wellington 7 1,511 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,662 Sep-06-2021, 03:43 PM
Last Post: muzikman
  'namespace' shorthand for function arguments? shadowphile 5 2,646 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,197 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Why Pass Functions as arguments? muzikman 14 5,798 Jan-18-2021, 12:08 PM
Last Post: Serafim
  Function won't apply dynamically in timeseries illmattic 1 1,761 Jan-08-2021, 03:15 PM
Last Post: stullis
  how to pass arguments between pythons scripts? electricDesire 2 2,195 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Do I have to pass 85 variables to function? Milfredo 10 4,365 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,585 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