# Running MaximizeOverPermutations in Python using WolframClient

**URL:** <https://community.wolfram.com/t/running-maximizeoverpermutations-in-python-using-wolframclient/21862>\
**Category:** Questions\
**Tags:** external-interfaces, optimization, wolfram-cloud, wolfram-engine\
**Created:** [April 10, 2023, 4:31am UTC](https://community.wolfram.com/t/running-maximizeoverpermutations-in-python-using-wolframclient/21862 "2023-04-10T04:31:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramk3](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@ramk3](https://community.wolfram.com/u/ramk3)\
**Post date:** [April 10, 2023, 4:31am UTC](https://community.wolfram.com/t/running-maximizeoverpermutations-in-python-using-wolframclient/21862/1 "2023-04-10T04:31:07Z")

</div>

Hello,  
I am trying to use the MaximizeOverPermutations over permutations function in Python as I have to use this for an optimization problem. I am new to using Wolfram client and wolfram functions and I am not sure how to run this example in Python.

```
f[{1, 2, 3}] = 2;
f[{1, 3, 2}] = 9;
f[{2, 1, 3}] = 1;
f[{2, 3, 1}] = -3;
f[{3, 1, 2}] = 9;
f[{3, 2, 1}] = 5;

ResourceFunction["MaximizeOverPermutations"][f, 3]

```

I am running the following code but there is obviously something wrong with the syntax:

```
from wolframclient.language import wl
from wolframclient.evaluation import WolframLanguageSession

session = WolframLanguageSession('C:\\Program Files\\Wolfram Research\\Mathematica\\13.2\\WolframKernel.exe')
f[{1, 2, 3}] = 2;
f[{1, 3, 2}] = 9;
f[{2, 1, 3}] = 1;
f[{2, 3, 1}] = -3;
f[{3, 1, 2}] = 9;
f[{3, 2, 1}] = 5;
m = session.evaluate(wl.ResourceFunction["MaximizeOverPermutations"][f, 3])
print(m)
session.terminate()

```

Can someone please explain how to run this example in Python?

---

<div class="post-metadata">

**Author:** ![robpacey](https://avatars.discourse-cdn.com/v4/letter/r/51bf81/32.png) [@robpacey](https://community.wolfram.com/u/robpacey)\
**Post date:** [November 2, 2025, 10:26pm UTC](https://community.wolfram.com/t/running-maximizeoverpermutations-in-python-using-wolframclient/21862/2 "2025-11-02T22:26:50Z")

</div>

Ram,

That is a great question about using the [Wolfram Client Library for Python](https://reference.wolfram.com/language/WolframClientForPython/) to execute specialized [Wolfram Language functions](https://resources.wolframcloud.com/FunctionRepository)!

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}**.
