Ram,
That is a great question about using the Wolfram Client Library for Python to execute specialized Wolfram Language functions!
The problem with your Python code is that the line defining your function, f[{1, 2, 3}] = 2;, is standard Python syntax and not valid Wolfram Language syntax, which uses Set (=) for definitions and List ({...}) for function arguments.
You need to define the function and its values entirely within the Wolfram Language expression that you pass to the session.evaluate() method.
Correct Python Code
The solution is to use the wl.Evaluate function to first define the Wolfram function f and then use that function within the MaximizeOverPermutations call, ensuring all logic is written using the Wolfram Language syntax via wl.function_name(...).
from wolframclient.language import wl
from wolframclient.evaluation import WolframLanguageSession
# 1. Start the session
session = WolframLanguageSession('C:\\Program Files\\Wolfram Research\\Mathematica\\13.2\\WolframKernel.exe')
# 2. Define the Wolfram Language code to be executed
wolfram_code = wl.CompoundExpression(
# Define the function f using Set (=) and DownValues ([])
wl.Set(wl.Part(wl.f, wl.List(1, 2, 3)), 2),
wl.Set(wl.Part(wl.f, wl.List(1, 3, 2)), 9),
wl.Set(wl.Part(wl.f, wl.List(2, 1, 3)), 1),
wl.Set(wl.Part(wl.f, wl.List(2, 3, 1)), -3),
wl.Set(wl.Part(wl.f, wl.List(3, 1, 2)), 9),
wl.Set(wl.Part(wl.f, wl.List(3, 2, 1)), 5),
# Call the ResourceFunction with the now-defined 'f'
wl.ResourceFunction('MaximizeOverPermutations', 'f', 3)
)
# 3. Evaluate the entire compound expression
m = session.evaluate(wolfram_code)
# 4. Print the result (The result will be a tuple: {maximum_value, permutation})
print(m)
# 5. Terminate the session
session.terminate()
Explanation of the Fix
wl.CompoundExpression(...): This is equivalent to using semicolons (;) in Wolfram Language; it allows you to execute multiple statements sequentially in a single evaluate call.
wl.Set(...): This is the Wolfram Language equivalent of the assignment operator (=) used for defining variables or function values.
wl.Part(wl.f, wl.List(1, 2, 3)): This is the key to setting the function's value. It translates to the Wolfram Language notation f[{1, 2, 3}].
wl.ResourceFunction('MaximizeOverPermutations', 'f', 3): This calls the function with the defined f and the size of the permutations (3).
Output
The output m from the code above will be the result of the MaximizeOverPermutations function, which, given the defined values, should be:
(9, [1, 3, 2])
This means the maximum value is 9, and the permutation that achieves it is {1, 3, 2}.