3

The following works in Python 3.8+

a = 1.5
print(f'{a=}')

In earlier Python versions it is equivalent to

a = 1.5
print(f'a={a}')

I developed my library for Python 3.8+ but a few servers (CentOS-7, OpenSUSE-15.1/15.2) have Python 3.6 by default, where this library is to be deployed. I can install 3.8 on these servers as last resort or rewrite f-string lines.

I was wondering if there is any from __future__ import xxx trick or a third party library that backports this to 3.6.

mkrieger1
  • 10,793
  • 4
  • 39
  • 47
Dilawar
  • 4,808
  • 9
  • 37
  • 54
  • 1
    Not possible, since this is a breaking syntax change. – MegaIng Dec 14 '20 at 21:46
  • There are probably way around it, I got excited by looking at this https://github.com/asottile/future-fstrings but this doesn't solve my problem. – Dilawar Dec 14 '20 at 21:49
  • Just checking the version number and inserting that case for f-strings won't work either I'm guessing? – Steven Dec 14 '20 at 21:51
  • Yes, you can do a lot of black magic and essentailly make anything in python (and thank you for that link, it showed me a new trick). But don't. This isn't valid python3.6 code, and it will throw a syntax error if executed normally. The package you linked circumvents the interpreter and manually pre parses the file to change it contents. Just don't do this kind of stuff anywhere near production. Also, this question is clearly offtopic. – MegaIng Dec 14 '20 at 21:54
  • @MegaIng Breaking syntax changes is sort of the purpose of future imports, but it is indeed to enable *planned* behavior before it becomes mandatory. Future imports aren't retroactively added to older versions if they aren't already there. – chepner Dec 14 '20 at 22:15
  • @chepner Yes. And so it is impossible to add to python3.6. – MegaIng Dec 14 '20 at 22:22
  • 1
    Getting a syntax error with 3.7.1. How come? – Tomerikoo Dec 15 '20 at 12:12
  • 1
    @Tomerikoo Because it is python3.8+ – MegaIng Dec 15 '20 at 13:39

1 Answers1

2

Here you go: https://github.com/MegaIng/python-magic/tree/master/encoding_magic

While I don't really recommend using this kind of code in production, this implements what you want for python3.6.

Note that this package isn't published on pypl since it would be problematic to install it correctly without manual setup afterwards.

This works the same as the other package you point me to, but it doesn't allow fstrings to work in versions < 3.6, only implements the = for python3.6.

MegaIng
  • 5,306
  • 1
  • 13
  • 30