# Should I avoid using unicode in names of variables?

Hello, Sage community!

Since Sage now supports Python 3, the possibility of using tildes and other unicode letters in names of variables is real. For example, I could define a constant Gauß instead of Gauss (with a "ß" instead of "ss"); or I could define a function called Müller (with two dots over the "u"). In Spanish and other languages, this is a more pressing matter. If I defined a function design(howMany), in Spanish it would be diseño(cuántos).

However, it seems quite strange to have tildes, betas, etc. in variable names. Is it a good programming behavior to avoid this kind of names?, or, should I use them sparely?

edit retag close merge delete

Sort by » oldest newest most voted

See python's PEP-8 for the policy for python's standard library (identifiers must be ASCII) and PEP-3131 for the proposal that led to unicode identifiers being possible. Sage library code has more-or-less been following PEP-8, so I expect non-ascii identifiers will not be allowed there. What you do in your own files is your own concern (go wild and name your eigenvalue $\lambda$).

Non-ascii unicode characters may be difficult to type (depending on keyboard layout) and they can be extremely confusing, because unicode contains different code-points that have (nearly) identical print-shapes. I'd be strongly in favour for keeping them out of library code.

more

I'm all for it--in fact I've been hoping that Sage will start to come with more unicode variables built-in (e.g. for pi). Also; neat trick available in Python 3 only that most people don't know: You can tab-complete unicode characters e.g. by typing \pi<tab>. Unfortunately it doesn't work well with some simple accented characters. For example to complete ó you have to type (afaict) \LATIN SMALL LETTER O WITH ACUTE<tab>. That should be fixed somehow...

( 2020-01-07 08:33:33 -0500 )edit

Ah, I figured it out, you can type o\acute<tab>. Similarly u\ddot<tab> gives you ü, etc. So you can either do the full unicode character name, or you can use its latex name if it has one (e.g. \ss gives ß), but for latex commands that modify a character, you type the character firstthen the command, then tab. This is documented at https://ipython.readthedocs.io/en/sta...

( 2020-01-07 08:35:44 -0500 )edit

I would not be pleased if I were to be forced to type gro\ddot<tab>bner_basis. Also, whatever tab completion exists in interactive shells is unlikely to be available in many code editors, so relying on tab completion would make for a very poor "transportable" sage experience. I think keyboards worldwide are just not very unicode capable. Given the prevalence of english as an international language, I suspect this won't change very quickly. Perhaps the situation will change when mandarin takes over.

( 2020-01-07 11:26:36 -0500 )edit

In Python 3 you can do some nice things like:

sage: π = pi
sage: sin(π)
0
sage: cos(π)
-1


and so on. For Python 3 builds of Sage it would be nice to have a few of these built-in. Per my comment on Nil's answer, special characters can be input fairly easily by using tab-completion.

more

That's true for the "sage console" you get via the "sage" command in a Unix terminal. And in "our" Jupyter notebook. Nice...

Not sure how to get something equivalent in my favorite interface (emacs + sage-shell-mode)...

( 2020-01-07 10:51:53 -0500 )edit

You would have to look into what unicode completion plugins exist for emacs (I'm sure there are several).

( 2020-01-07 10:54:30 -0500 )edit

I just tried out the tab-completion on "\pi". It works for me in IPython but not in plain Python. (This is with Python 3.7.3, built via Sage 9.0.)

( 2020-01-07 12:52:23 -0500 )edit

Of course--this is an IPython feature.

( 2020-01-08 09:13:59 -0500 )edit

It would be really nice to be able to have constants like $\pi$, $\gamma$ and $\phi$. For example, I would like to write sin(π) instead of sin(pi), or ϕ instead of golden_ratio.

Can we do this a feature of Sage? Maybe a separate module could define this (I could do it, if the developers agree). Notice that the Julia language has a similar feature.

( 2020-01-08 09:32:46 -0500 )edit