issue147:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue147:python [2019/07/29 23:21] – créée d52fr | issue147:python [2019/08/08 10:21] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | As some of you may know, Python 3.8.0b2 was released on July the fourth. It is available for download at https:// | + | **As some of you may know, Python 3.8.0b2 was released on July the fourth. It is available for download at https:// |
- | There are a lot of changes in version 3.8.0, one of which we have already looked into back in April (FCM #144), Positional-only arguments. Many have to do with CPython and things that some of us won’t ever need to worry about. Here is a list of a few of the other things that are new: | + | Comme vous pouvez le savoir, Python 3.8.0b2 a été publié le 4 juillet. Il est disponible pour téléchargement sur https:// |
+ | |||
+ | **There are a lot of changes in version 3.8.0, one of which we have already looked into back in April (FCM #144), Positional-only arguments. Many have to do with CPython and things that some of us won’t ever need to worry about. Here is a list of a few of the other things that are new: | ||
• Assignment Expressions - PEP 572 (see below) | • Assignment Expressions - PEP 572 (see below) | ||
• Pickle protocol 5 with out-of-band data - PEP 574 | • Pickle protocol 5 with out-of-band data - PEP 574 | ||
Ligne 13: | Ligne 15: | ||
PEP 589 (TypedDict) | PEP 589 (TypedDict) | ||
- | I'm going to focus on two of the changes in this article. First, we'll take a look at the Assignment Expression change. | + | I'm going to focus on two of the changes in this article. First, we'll take a look at the Assignment Expression change.** |
- | In this change, there is an additional operator for us. It's called the " | + | Il y a beaucoup de modifications dans la version 3.8.0 et nous avons déjà vu l'une d' |
+ | ••Expressions d' | ||
+ | ••Protocole 5 de Pickle avec des données « out-of-band » - PEP 574 | ||
+ | ••Absence d' | ||
+ | ••Support pratique des f-strings = spécifieur pour le débogage (voir ci-dessous) | ||
+ | ••LOAD_GLOBAL est maintenant 40 % plus rapide | ||
+ | ••pickle utilise maintenant le protocole 4 par défaut | ||
+ | ••En lien avec la saisie : | ||
+ | PEP 591 (Qualifieur final), | ||
+ | PEP 586 (Type de literal) | ||
+ | PEP 589 (TypedDict) | ||
+ | |||
+ | Je vais me concentrer sur deux types de modification dans cet article. D' | ||
+ | |||
+ | **In this change, there is an additional operator for us. It's called the " | ||
Assume we have a list. We'll call it " | Assume we have a list. We'll call it " | ||
Ligne 29: | Ligne 45: | ||
print(' | print(' | ||
- | You can see that it saves us a line of code. | + | You can see that it saves us a line of code.** |
- | Here (top) is another example, a bit more "real world" | + | Dans cette modification, |
+ | |||
+ | Partons de l' | ||
+ | n = len(lst) | ||
+ | |||
+ | if n > 10 : | ||
+ | print(' | ||
+ | |||
+ | Maintenant, nous utiliserons le nouvel opérateur walrus. Souvenez-vous que l'on doit avoir Python 3.8.0 pour lancer ce code : | ||
+ | |||
+ | if (n := len(lst)) > 10: | ||
+ | print(' | ||
+ | |||
+ | Vous voyez ; nous avons gagné une ligne de code. | ||
+ | |||
+ | **Here (top) is another example, a bit more "real world" | ||
Here we import the json library and create a string compliant with json containing a number of city/state, latitude and longitude entries. We then use the json.loads load string method assigning it to a variable called locations. We then get the list of entries into the variable ' | Here we import the json library and create a string compliant with json containing a number of city/state, latitude and longitude entries. We then use the json.loads load string method assigning it to a variable called locations. We then get the list of entries into the variable ' | ||
Ligne 41: | Ligne 72: | ||
The output from this short program would be… | The output from this short program would be… | ||
+ | |||
+ | Odessa, Texas, USA | ||
+ | 31.84 | ||
+ | -102.36** | ||
+ | |||
+ | Voyez ci-dessus un autre exemple, un peu plus « monde réel ». | ||
+ | |||
+ | Ici, nous importons une bibliothèque json et créons une chaîne compatible avec json contenant un certain nombre d' | ||
+ | |||
+ | l = locations[" | ||
+ | |||
+ | Ensuite (page suivante, en haut à droite), nous parcourons la liste, une entrée à la fois, et nous utilisons l' | ||
+ | |||
+ | La sortie de ce court programme serait : | ||
Odessa, Texas, USA | Odessa, Texas, USA | ||
Ligne 46: | Ligne 91: | ||
-102.36 | -102.36 | ||
- | The other new item I wanted to discuss deals with the f-strings formatting addition. It’s basically an aid for using print when debugging your code. F-strings were introduced in Python 3.6 and is the third formatting option for strings along with the “% formatting” option (which goes WAY back) and the “str.format()” option which goes back to Python 2.6. | + | **The other new item I wanted to discuss deals with the f-strings formatting addition. It’s basically an aid for using print when debugging your code. F-strings were introduced in Python 3.6 and is the third formatting option for strings along with the “% formatting” option (which goes WAY back) and the “str.format()” option which goes back to Python 2.6. |
I assume you all understand the % formatting option – I’ve dealt with it since my first few articles. I’m sure most of you have used the str.format() option as well, but just in case, here is a quick recap. | I assume you all understand the % formatting option – I’ve dealt with it since my first few articles. I’m sure most of you have used the str.format() option as well, but just in case, here is a quick recap. | ||
Ligne 59: | Ligne 104: | ||
In the % formatting methodology, | In the % formatting methodology, | ||
- | print(" | + | print(" |
- | Which would produce: | + | L' |
+ | |||
+ | Je pars du principe que vous comprenez tous l' | ||
+ | |||
+ | Disons que vous voulez créer une chaîne pour l' | ||
+ | |||
+ | mag = "Full Circle Magazine" | ||
+ | issue = " | ||
+ | month = " | ||
+ | year = " | ||
+ | |||
+ | Dans la méthodologie de format %, vous utiliserions : | ||
+ | |||
+ | print(" | ||
+ | |||
+ | **Which would produce: | ||
Written for Full Circle Magazine issue #147 July, 2019 | Written for Full Circle Magazine issue #147 July, 2019 | ||
Ligne 71: | Ligne 131: | ||
Which provides the same output. Notice that the curly brackets act as place holders for the variables in the .format() portion of the statement. When using the curly brackets, you can either leave them empty (as above) or provide an “index” number which relates to the index within the format statement like this… | Which provides the same output. Notice that the curly brackets act as place holders for the variables in the .format() portion of the statement. When using the curly brackets, you can either leave them empty (as above) or provide an “index” number which relates to the index within the format statement like this… | ||
- | print(' | + | print(' |
- | Again, this produces the same output as the others. Yet another way to do this is to do the following: | + | Ce qui produit : |
+ | |||
+ | Écrit pour Full Circle Magazine numéro #147 July, 2019 | ||
+ | |||
+ | Pour utiliser la méthode str.format(), | ||
+ | |||
+ | print(' | ||
+ | |||
+ | qui fournit la même sortie. Notez que les accolades tiennent lieu de points de placement pour les variables de la portion de la déclaration .format(). Quand les accolades sont utilisées, vous pouvez, soit laisser vide (comme ci-dessus), soit fournir un nombre « index » qui est lié à l' | ||
+ | |||
+ | print(' | ||
+ | |||
+ | |||
+ | **Again, this produces the same output as the others. Yet another way to do this is to do the following: | ||
print(' | print(' | ||
Ligne 83: | Ligne 156: | ||
print(f" | print(f" | ||
- | As you can see, it is much shorter (and readable), since we simply place an “f” before the opening quote and use the variable names within the curly brackets, forgetting about the ‘.format()’ porton. | + | As you can see, it is much shorter (and readable), since we simply place an “f” before the opening quote and use the variable names within the curly brackets, forgetting about the ‘.format()’ porton.** |
- | Now for the new part. Python 3.8 gives us the ability of using an ‘=’ sign. As I said above, this is mainly for debugging support. Assuming the variable assignments above, if we want to print the value of the mag variable, we could simply do it like this… | + | Encore une fois, cela produit la même sortie que les autres. Encore une façon différente de faire dans ce qui suit : |
+ | |||
+ | print(' | ||
+ | |||
+ | Cependant, ceci est un peu pataud, et comme vous pouvez le voir, rend longue la déclaration quand plusieurs variables/ | ||
+ | |||
+ | Maintenant, utilisons les f-strings. L' | ||
+ | |||
+ | print(f" | ||
+ | |||
+ | Comme vous pouvez le voir, c'est plus court (et lisible), car nous plaçons simplement un « f » avant les guillemets ouvrants et utilisons les noms de variables dans des accolades, oubliant la portion du « .format() ». | ||
+ | |||
+ | **Now for the new part. Python 3.8 gives us the ability of using an ‘=’ sign. As I said above, this is mainly for debugging support. Assuming the variable assignments above, if we want to print the value of the mag variable, we could simply do it like this… | ||
print(mag) | print(mag) | ||
Ligne 101: | Ligne 186: | ||
mag=' | mag=' | ||
- | This is so much easier to read in the debugging output in the terminal than just the previous output. | + | This is so much easier to read in the debugging output in the terminal than just the previous output.** |
- | There are so many new things that Python 3.8 offers us. While compiling Python on your own can be a challenge, there are many websites that offer step-by-step instructions to do this. Remember, however, this is a beta product, so there are bound to be issues. You might want to wait a few months until one of the release candidates become available. It would also be a good idea to create yourself some sort of virtual environment to support your 3.8 work without risking breaking anything you currently have. Consider it a “sandbox”. | + | Maintenant, pour la partie nouvelle, Python 3.8 nous donne la possibilité d' |
+ | |||
+ | print(mag) | ||
+ | |||
+ | Et, comme nous le savons tous, ça imprimera : | ||
+ | |||
+ | Full Circle Magazine | ||
+ | |||
+ | Mais, si nous utilisons la nouvelle option = fournie par Python 3.8, nous pouvons utiliser ce qui suit : | ||
+ | |||
+ | print(f" | ||
+ | |||
+ | Cela affichera : | ||
+ | |||
+ | mag=' | ||
+ | |||
+ | C'est tellement plus facile à lire en sortie de débogage sur le terminal que la seule sortie précédente. | ||
+ | |||
+ | **There are so many new things that Python 3.8 offers us. While compiling Python on your own can be a challenge, there are many websites that offer step-by-step instructions to do this. Remember, however, this is a beta product, so there are bound to be issues. You might want to wait a few months until one of the release candidates become available. It would also be a good idea to create yourself some sort of virtual environment to support your 3.8 work without risking breaking anything you currently have. Consider it a “sandbox”. | ||
For a detailed list of the upcoming features in Python 3.8.0, see https:// | For a detailed list of the upcoming features in Python 3.8.0, see https:// | ||
Ligne 109: | Ligne 212: | ||
Python 3.8.0 is currently expected to be released on 10/21/19. | Python 3.8.0 is currently expected to be released on 10/21/19. | ||
- | Until next time, happy coding! | + | Until next time, happy coding! |
+ | |||
+ | Il y a tant de choses nouvelles qui nous sont proposées par Python 3.8. Alors que la compilation de Python par vous-même peut s' | ||
+ | |||
+ | Pour une liste détaillée des fonctionnalités qui arrivent dans Python 3.8, voyez https:// | ||
+ | |||
+ | La publication de Python 3.8.0 est actuellement prévue le 21/10/19. | ||
+ | |||
+ | Jusqu' | ||
+ | |||
issue147/python.1564435292.txt.gz · Dernière modification : 2019/07/29 23:21 de d52fr