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?
#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


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

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