Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating symbols from Python #156

Closed
ChrisRackauckas opened this issue Apr 23, 2018 · 6 comments
Closed

Creating symbols from Python #156

ChrisRackauckas opened this issue Apr 23, 2018 · 6 comments

Comments

@ChrisRackauckas
Copy link

Is this possible? I would like a user to be able to pass a symbol from Python to a Julia API. Do I instead need to implement a conversion from a string for comparisons? That sounds dangerous.

@stevengj
Copy link
Member

Call the Julia Symbol(string) function?

@ChrisRackauckas
Copy link
Author

ChrisRackauckas commented Apr 24, 2018

>>> import julia
>>> jul = julia.Julia()

>>> jul.Symbol("x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Julia' object has no attribute 'Symbol'
>>> jul.symbol("x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Julia' object has no attribute 'symbol'

@stevengj
Copy link
Member

stevengj commented Apr 24, 2018

That's #144. For now you probably need jul.eval("Symbol")("x")

But a separate problem is that it might convert it back to a string when it returns the value to Python, since PyCall by default converts Julia Symbols to Python strings.

@stevengj
Copy link
Member

stevengj commented Apr 24, 2018

In Julia, you would currently have to do PyCall.pyjlwrap_new(:x) to create a Python wrapper for a Julia Symbol. So maybe jul.eval("PyCall.pyjlwrap_new(:x)")?

(I'm guessing slightly here because I don't use pyjulia myself, so I'm working off of how I remember it working and how PyCall works in Julia.)

@ChrisRackauckas
Copy link
Author

>>> jul.eval("a=:x")
'x'
>>> jul.eval("typeof(a)")
<PyCall.jlwrap Symbol>

I can use that to define a jul.symbol("x") function. Thanks.

But a separate problem is that it might convert it back to a string when it returns the value to Python, since PyCall by default converts Julia Symbols to Python strings.

This is only going to be used Python->Julia, so it's safe. I think I got all I need now, other than JuliaPy/PyCall.jl#497 and the tag. It worked out surprisingly smooth.

@iffanh
Copy link

iffanh commented Feb 3, 2023

In Julia, you would currently have to do PyCall.pyjlwrap_new(:x) to create a Python wrapper for a Julia Symbol. So maybe jul.eval("PyCall.pyjlwrap_new(:x)")?

(I'm guessing slightly here because I don't use pyjulia myself, so I'm working off of how I remember it working and how PyCall works in Julia.)

I know this is an old thread but this works for me, thanks!

new_var = jl.eval(f"PyCall.pyjlwrap_new(:{old_var})")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants