# Can the parameter of the Python.evaluate method only be a StringLiteral instead of a String?

**URL:** <https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314>\
**Category:** Mojo\
**Created:** [April 26, 2025, 3:49am UTC](https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314 "2025-04-26T03:49:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Treagzhao](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Treagzhao](https://forum.modular.com/u/Treagzhao)\
**Post date:** [April 26, 2025, 3:49am UTC](https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314/1 "2025-04-26T03:49:37Z")

</div>

```auto
fn initialize_with_zeros(dim: UInt) raises -> (PythonObject, UInt):
    var str = String("(12288,1)")
    print(str)
    print(str == "(12288,1)")
    var obj = Python.evaluate(str)
    return obj, Uint()

```

When I run this function，the compiler said `Error loading dataset: invalid non-printable character U+0001 (<string>, line 1)`.  
 ![image](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/c873ae81db42b01187685edcf6071d7333902beb.png)

```auto
fn initialize_with_zeros(dim: UInt) raises -> (PythonObject, UInt):
    var str = "(12288,1)"
    print(str)
    print(str == "(12288,1)")
    var obj = Python.evaluate(str)
    var np = Python.import_module("numpy")
    var w = np.zeros(obj)
    var b: UInt = 0
    print("w", w)
    return w, b

```

The only differences between this code and above, is that the variable `str` is `StringLiteral`. And this code runs perfectly.  
The argument of `Python.evaluate` only support `StringLiteral`? What if i have to parse a variable into `PythonObject`?

---

<div class="post-metadata">

**Author:** ![sora](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sora](https://forum.modular.com/u/sora)\
**Post date:** [April 26, 2025, 4:21am UTC](https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314/2 "2025-04-26T04:21:56Z")

</div>

Could you open an issue on GitHub? I think it’s a null-correctness bug: `Python.evaluate` assumes the input value is null-terminated.

```mojo
from python import Python

fn main() raises:
  var s = String("(100, 1)")
  # s.append_byte(0) # uncommenting this line fixes the issue
  print(Python.evaluate(s))

```

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [April 26, 2025, 5:12pm UTC](https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314/3 "2025-04-26T17:12:17Z")

</div>

Thanks for noticing this, [it should be fixed](https://github.com/modular/max/issues/4431) in the next nightly.

---

<div class="post-metadata">

**Author:** ![Treagzhao](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Treagzhao](https://forum.modular.com/u/Treagzhao)\
**Post date:** [April 27, 2025, 1:08am UTC](https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314/4 "2025-04-27T01:08:47Z")

</div>

Thank you for replying so promptly.I am researching on mojo, and I maybe will have a lot of questions in the future.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [May 4, 2025, 1:09am UTC](https://forum.modular.com/t/can-the-parameter-of-the-python-evaluate-method-only-be-a-stringliteral-instead-of-a-string/1314/5 "2025-05-04T01:09:18Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
