0

I want to use single quote in a variable name and keyword. I know how to do this for a variable value using single and double quotes("5'_Exon_Annotation") or using black slash to escape the single quote. But not sure how to do this with variable name/ key word. Below are examples of how I want to assign values to column names and a variable that needs single quote.

    5'co-ordinate = "something" 

    gene_df = gene_df.assign(gene_position = gene_pos,
                             5'_co-ordinate =  bkpt1_list[1],
                             3'_co-ordinate = bkpt2_list[1])

First of all is it possible in Python? I couldn't find a post related to this.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gopinath S
  • 511
  • 6
  • 20
  • 7
    This is not possible. One of the rules for Python variables: A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). – Scott Boston May 18 '20 at 21:05
  • 1
    An idea is to spell out `prime` or `_prime_` instead of the single quote. – Arne May 18 '20 at 21:08
  • That is not possible as Scott said. though maybe you can use a dictionary's key value pair. – shivankgtm May 18 '20 at 21:08
  • @ScottBoston It actually is kind of possible using `globals()` or `locals()`, see my answer. – mapf May 18 '20 at 21:23
  • 1
    Python object names are not limited to alphanumeric. Since Python 3 you can use Unicode characters like `déjà_vu = 42;print(déjà_vu)` or `あいうえお='Japanese vowels';print(あいうえお)`. This makes it easy for non-English beginners to name objects, but it would block people to participate in a project if the character is not available on their keyboard ! For this question you would tempted to use characters that look like quotes like U+2018 LEFT SINGLE QUOTATION MARK, but they are forbidden by Python syntax, to prevent security problems. See PEP-3131 for more https://www.python.org/dev/peps/pep-3131/ – Mickaël Bucas May 19 '20 at 17:20

2 Answers2

2

While I wouldn't recommend it at all, it is possible to use all kinds of characters for attributes of classes using setattr(), like so:

class Test:
    def __init__(self):
        setattr(self, 'this variable has ""', 1)


test = Test()
print(getattr(test, 'this variable has ""'))

# test.this variable has "" does NOT work

But I really wouldn't recommend it. The intestesting thing about this is, that setting an attribute this way makes it kind of hidden, because you can only access it using getattr().

If you use globals() or locals() you can also set variables this way:

locals()['this variable has ""'] = 1  # or globals()
print(locals()['this variable has ""'])

# this variable has "" = 2 does NOT work

That makes it really cumbersome to access the variable though, and again, I really wouldn't recommend it. It's a neat trick (debateable I guess) if you really want to hide something though.

mapf
  • 1,906
  • 1
  • 14
  • 40
1

Here are the available characters for Python identifiers. You might find something which is close to an apostrophe but not the common one.

Btw. I'd not suggest to deviate from the ASCII table. If the language naturally support it and the editors offer helpers to write those, it's fine (like Julia) but for Python it can be quite confusing for others.

https://docs.python.org/3/reference/lexical_analysis.html#identifiers

Here is an example of a valid Python code using some kind of a quote in the function name (copy-paste it, but the single quote is for example not displayed in Safari on macOS, however it's there as you can see in the screenshot of my terminal):

In [1]: def f֜(x): return 2*x

In [2]: f֜(3)
Out[2]: 6

Unicode char in function name

tamasgal
  • 24,826
  • 18
  • 96
  • 135
  • 1
    huh? Python works with any utf-8 source code just fine - it's the default. And really, alternate encodings can be used if declared at the top of the source code file. – juanpa.arrivillaga May 18 '20 at 21:09
  • Yes, but the single quote is reserved and not valid in identifier names. I just added an example. – tamasgal May 18 '20 at 21:11