0

Let's say I've got two variables: "choice" = ii7 and "scale" = C.

This library has a function to figure out notes in any particular musical chord by running this:

chords.ii7(scale)

When scale = C, it'll list the notes of the ii7 chord in the scale of C.

If I had to use the variable "choice" in place of including ii7 in the code itself, how could I go about it? It would definitely be a lot easier to search if I knew what to search for, but I'm a total beginner. This is just to top off the rest of this particular program, and learn something new. Apologies if I worded the title wrong. Thanks in advance!

1 Answer 1

1

Use getattr

getattr(chords, choice)(scale)

This is assuming choice = 'ii7'.

getattr basically takes two parameters - the first being an object, the second being a string. It searches for an attribute that has the same name as the string, and returns it. In this case getattr(chords, choice) returns chords.ii7, which you then have to call.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, solved my problem. Really appreciate the help! Definitely going to look more into why that works the way it does, thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.