I want to know the equivalent of the julia type "symbols" in Python because i am trying to translate a code that i am working on, from julia to Python. Thank you
Asked
Active
Viewed 795 times
1
-
1To answer the question, we need to know whether you are using `Symbol` types because they are [interned strings](https://en.wikipedia.org/wiki/String_interning) or whether you are using them for their ability to represent Julia code itself, e.g. in conjunction with the `Expr` type, meta-programming, `@eval` e.t.c. If you're not sure, then it is *very* likely you are just using them as interned strings in which case the answer provided by @thebjorn is perfectly satisfactory (or if speed doesn't matter you can just replace them with strings, as @NedBatchelder suggests). – Colin T Bowers Apr 16 '19 at 00:14
2 Answers
0
The intern(s)
function may do what you want:
>>> help(intern)
Help on built-in function intern in module __builtin__:
intern(...)
intern(string) -> string
``Intern'' the given string. This enters the string in the (global)
table of interned strings whose purpose is to speed up dictionary lookups.
Return the string itself or the previously interned string object with the
same value.
>>>

thebjorn
- 26,297
- 11
- 96
- 138
-
what aboiut if i used only strings instead of symbols, i think it's convinient too. – M.HANHASSE Apr 16 '19 at 05:56
-
See Colin's comment on your question for reasons to chose one over the other. Strings are definitely convenient (`intern` returns a string, but such that `intern('hello' + ' ' + 'world') is intern('hello world')` -- notice the `is` which is faster than `==`). If you don't care about that kind of performance, but need more functionality then creating a subclass of `str` might be a solution. It really all depends on what you're going to use them for... – thebjorn Apr 16 '19 at 06:24
-
-
@NedBatchelder how so? (`"hello " + "world" is "hello world"` returns False, while wrapping in `sys.intern(..)` returns True). – thebjorn Apr 16 '19 at 18:41
-
1Ah, this gets subtle, as undocumented implementation details often do. If the string is a valid identifier, then it's interned: `"he"+"llo" is "hell"+"o"` --> True. Better not to depend on it. – Ned Batchelder Apr 16 '19 at 19:49
0
Use strings instead. What do symbols do that strings don't do?

Ned Batchelder
- 364,293
- 75
- 561
- 662
-
2Regarding what symbols do, see: https://stackoverflow.com/questions/23480722/what-is-a-symbol-in-julia. – mbauman Apr 15 '19 at 18:04
-
That post seems to indicate that symbols are important for representing the program as data, which I suspect most actual programs are not doing. We'll need to hear from the OP about what symbols are being used for in their program. – Ned Batchelder Apr 15 '19 at 23:41
-
-
i'm still quit new on Julia for example one of the things that i didn't understand about one of Symbols commads is this one: `aH = symbols(printf("a_H1:%d", n+1))` – M.HANHASSE Apr 16 '19 at 10:25
-
It will be hard to help one line at a time. We'd need to see what `aH` is used for. – Ned Batchelder Apr 16 '19 at 10:58
-
okay here is a part of the julia code that i am trying to translate to Python : `mutable struct QRelaySym{T<:Tuple} aH::T bH::T aV::T bV::T apH::T bpH::T apV::T bpV::T end ` – M.HANHASSE Apr 16 '19 at 16:18