# Accessing Objects Created in PyScript from JavaScript

**URL:** <https://forum.anaconda.com/t/accessing-objects-created-in-pyscript-from-javascript/31551>\
**Category:** PyScript\
**Created:** [August 18, 2022, 3:37pm UTC](https://forum.anaconda.com/t/accessing-objects-created-in-pyscript-from-javascript/31551 "2022-08-18T15:37:49Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![JeffGlass](https://dub1.discourse-cdn.com/anaconda/user_avatar/forum.anaconda.com/jeffglass/32/1301_2.png) [@JeffGlass](https://forum.anaconda.com/u/JeffGlass)\
**Post date:** [August 19, 2022, 1:33pm UTC](https://forum.anaconda.com/t/accessing-objects-created-in-pyscript-from-javascript/31551/3 "2022-08-19T13:33:18Z")

</div>

I’ve found a way of binding a JavaScript variable to a Python object created in PyScript by using Javascript’s `eval()` method. I can’t imagine this is the best way to do this, but I thought it was interesting, and maybe useful to others exploring related topics down the road.

We create a Javascript function that takes an object and a string, and uses eval() to bind that object to a new variable with that string as it’s name.

```javascript
<script>
    function createObject(x, variableName){
        let execString = variableName + " = x"
        console.log("Running `" + execString + "`");
        eval(variableName + " = x")
    }
</script>

```

Then in PyScript, we call that function with the object’s we’d like to pass and their name

```python
<py-script>
    import js
    from pyodide import create_proxy, to_js

    names = ["Jeff Glass"]
    js.createObject(create_proxy(names), "names")

    multiplier = lambda z: z * 2
    js.createObject(create_proxy(multiplier), "multiplier")

    console.log(to_js(js.names))
    console.log(js.multiplier(5))
</py-script>

```

![image](https://europe1.discourse-cdn.com/anaconda/original/2X/a/a3045cc622c1eb4fe71b3f5d03f18f84b3f8b2e3.png)

Then if we mutate any of the Python objects, we can see the change reflected in JavaScript.

Of course the Javascript variable is bound to the Python _object_, not the Python variable name. Here:

- the list `names` is mutated, but
- the variable python variable `multiplier` is now points to the new lambda function
- while the Javascript variable `multiplier` still points to the original lambda function:

```auto
# continued from above
<py-script>
    names.append("John Hanley")
    console.log(to_js(js.names))

    multiplier = lambda z: z * 100000
    console.log(js.multiplier(5))
</py-script>

```

![image](https://europe1.discourse-cdn.com/anaconda/original/2X/b/b3444fa7699c2f65c5720e420399b7f520e216d1.png)

---

_[View the full topic](https://forum.anaconda.com/t/accessing-objects-created-in-pyscript-from-javascript/31551)._
