Store named expression's current value in a python variable

Member Posts: 3
Name Dropper First Comment First Anniversary
**

Dear All,

I can get the value of a named expression using this command:
solver.setup.named_expressions.compute(names = ["my_expression"])
Information is reported in the console.

Is there a way to store the value in a python variable?
**
For example:
**Input:

expression_value = command_that_I'm_looking_for
print(expression_value)
print(expression_value + 2000)

**Output: **
25
2025

Tagged:

Welcome!

It looks like you're new here. Sign in or register to get started.

Answers

  • Member, Employee Posts: 16
    10 Comments Second Anniversary Ansys Employee First Answer
    ✭✭✭
    edited February 14

    Hi Hristo,

    just try the following way to get that value stored in the variable

    1. val = solver.settings.setup.named_expressions["my_expression"].definition()
    2. print(val)

    definition returns the expression

    1. val = solver.settings.setup.named_expressions["my_expression"].get_value()
    2. print(val)

    get_value() evaluates and return the value

    Thanks!

  • Member, Employee Posts: 22
    Second Anniversary 10 Comments First Answer Ansys Employee
    ✭✭✭✭
    edited February 14

    @abhishekchitwar definition() should return the expression string (e.g., "2 + 2"). Is that what @Hristo wants or does he want the result, 4? That's via get_value:

    1. expr_val = solver.settings.setup.named_expressions["test_expr_1"].get_value()
  • Member Posts: 3
    Name Dropper First Comment First Anniversary
    **

    Hello, @abhishekchitwar and @Sean Pearson!

    .get_value() got me what I was looking for.

    Thank you!

Welcome!

It looks like you're new here. Sign in or register to get started.