0

I have been searching already but I have no idea how those e and j are called and therefore not found useful information. I am searching for numbers with chars for specification or as operators.

Like this exponetial e notaion:

1e3 = 1 * 10^(3) = 1000
xen = x * 10^(n)            # where x and n are numbers

or imaginary numbers with j:

1 + 3j

The benefit I am hoping for is to be able to write Ppm(2) (instance of Ppm class) conveniently as 2ppm (where "ppm" is "parts per million" of a frequency in NMR). This can't be thoroughly done with the exponential notation 2e-6 because ppm is referenced to a frequency. Hence it should be distinguished from ordinary numbers.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BadAtLaTeX
  • 614
  • 1
  • 9
  • 22
  • I don't think this is possible without modifying Python's source code. Numeric literal syntax like `3j` and `1e3` is effectively hard coded into the parser. Extending the parser to recognize `2ppm` as a literal as well, is beyond the reach of the ordinary user. – Kevin Feb 10 '15 at 14:43
  • They aren't "called"; they are part of the syntax for floating point and complex numbers. There is no way to extend that to custom classes. Python is not Ruby (although I'm not a Ruby expert, so I can't say if it would be possible there). – Tim Pietzcker Feb 10 '15 at 14:44
  • `e` is a [mathematical constant](https://docs.python.org/2/library/math.html#constants); imaginary numbers are [part of the number literal syntax](https://docs.python.org/2/reference/lexical_analysis.html#imaginary-literals). Neither is 'called'. You cannot alter Python grammar (at least not at runtime, and you'll have to compile your own Python binary if you tried to change the grammar). – Martijn Pieters Feb 10 '15 at 14:45
  • You can read the language specification, e.g. https://docs.python.org/2/reference/lexical_analysis.html#floating-point-literals, which tells you what literals are acceptable. You would have to create your own fork of Python, with the extra syntax/grammar rules, to make `2ppm` a *"`Ppm` literal"*. – jonrsharpe Feb 10 '15 at 14:46
  • 2
    @MartijnPieters I think they mean e.g. `1e2`, which is a valid [FP literal](https://docs.python.org/2/reference/lexical_analysis.html#floating-point-literals), rather than the constant. – jonrsharpe Feb 10 '15 at 14:47
  • @jonrsharpe: right, indeed, I didn't see that the OP is talking about exponents. That's part of the literal syntax too, so the *parser* does the work here. – Martijn Pieters Feb 10 '15 at 14:48
  • I've duped this to an excellent post on adding *statements*, but the same applies to adding expressions or literal notations. – Martijn Pieters Feb 10 '15 at 14:50
  • You might be interested in [Pint](https://pint.readthedocs.org/en/latest/tutorial.html) a 'Python package to define, operate and manipulate physical quantities'. – Peter Wood Feb 10 '15 at 14:53

1 Answers1

3

No, python does not support user defined literals.

horns
  • 1,843
  • 1
  • 19
  • 26