Ask Your Question
2

Should I avoid using unicode in names of variables?

asked 2020-01-05 02:23:51 +0200

dsejas gravatar image

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?

Thanks in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2020-01-05 09:36:00 +0200

nbruin gravatar image

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.

edit flag offensive delete link more

Comments

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...

Iguananaut gravatar imageIguananaut ( 2020-01-07 15:33:33 +0200 )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...

Iguananaut gravatar imageIguananaut ( 2020-01-07 15:35:44 +0200 )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.

nbruin gravatar imagenbruin ( 2020-01-07 18:26:36 +0200 )edit
2

answered 2020-01-07 15:41:29 +0200

Iguananaut gravatar image

updated 2020-01-08 18:27:43 +0200

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.

Ticket: https://trac.sagemath.org/ticket/28966

edit flag offensive delete link more

Comments

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)...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-01-07 17:51:53 +0200 )edit

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

Iguananaut gravatar imageIguananaut ( 2020-01-07 17:54:30 +0200 )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.)

John Palmieri gravatar imageJohn Palmieri ( 2020-01-07 19:52:23 +0200 )edit

Of course--this is an IPython feature.

Iguananaut gravatar imageIguananaut ( 2020-01-08 16:13:59 +0200 )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.

dsejas gravatar imagedsejas ( 2020-01-08 16:32:46 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-01-05 02:23:51 +0200

Seen: 915 times

Last updated: Jan 08 '20