Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

2 of 2
replaced http://stackoverflow.com/ with https://stackoverflow.com/
user avatar
user avatar

unicode identifiers without sacrificing python 2 compatibility

I have a python units package. I want to have both Å and angstrom be two aliases for angstroms, so that people can use whichever one they prefer (Å is easier to read but angstrom is easier to type). Since unicode identifiers are forbidden in Python 2, the Å option will obviously only be available in Python 3. My question is: Is there any way to have a single source file that works in both Python 2 and Python 3, and has this variable defined in Python 3 only?

The naive solution if sys.version_info >= (3,): Å = angstrom does not work, because Python 2 raises a syntax error.

Steve Byrnes
  • 2.3k
  • 1
  • 20
  • 25
lang-py

AltStyle によって変換されたページ (->オリジナル) /