Noplay >>Développement


Niveau: Débutant
Licence de l’article: Free Documentation License

version imprimable

Tester l’existence d’une variable en Python

le jeudi 28 décembre 2006 par
Noplay

Lorsque l’on débarque de langage plus permissif que Python comme PHP ou Perl on est surpris lorsqu’en essayant de tester l’existence d’une variable avec un simple if le programme s’arrete.


if toto:
    print toto
 
Traceback (most recent call last):
 File "./test.py", line 4, in ?
   if toto:
NameError: name 'toto' is not defined

En Python on part du principe qu’il est plus facile de demander pardon que de d’obtenir la permission. En effet si vous voulez utiliser une variable, Python va devoir tester en interne si cette variable existe. Si vous testez l’existence de la variable avant le test sera effectué deux fois. Une perte de temps non ? Python lève donc des exceptions en cas de problème.

Dans le cas qui nous intéresse il suffit donc de catcher l’exception NameError.


try:
  print toto
except NameError:
  print "Toto n'existe pas"
 


commentaires (17 message(s))

Buy Nexium Online
le 10 juin , par samuelfosss:

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

Buy Nexium Online | Buy Generic Drugs | Buy Aldara Online



Tester l’existence d’une variable en Python
le 9 juin , par Delia:

The posts are doing great and full of good insights. I would be glad to keep on coming back here to check for a new update. market research proposal | academic thesis research



Tester l’existence d’une variable en Python
le 7 juin , par Larry:

Well, at least you have managed to come out of it with only a few inconveniences. It could have been much worse. Tooway



Tester l’existence d’une variable en Python
le 7 juin , par Larry Page:

This variable cannot be found without the help of the Python library. This technique was used to see how the rca ieftin website works in reality.



Tester l’existence d’une variable en Python
le 5 juin , par blackhawk holsters :

The quick brown fox jumps over the lazy dog

blackhawk holsters



Order Meridia Online
le 31 mai , par ozonemattew:

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work !

Generic Aldara Online



Tester l’existence d’une variable en Python
le 27 mai , par Jake:

In practice the idea may provide a coherent underlying architecture on which to build processes that aid organizations and institutions to manage the social returns of their investments in a transparent and quantifiable way. MBNA



sonia
le 25 mai , par sonia:

Great, thanks for sharing this blog post. Fantastic. flower delivery in ukraine



Tester l’existence d’une variable en Python
le 22 avril , par :

Ce style d’écriture me plait bien. | régime rapide | regime rapide



liajames
le 18 avril , par :

Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us.flowers delivered in Canada|flowers online Australia



Tester l’existence d’une variable en Python
le 16 avril , par dfdsf:

There are plenty of reasons why you need to lose weight, eat right, and exercise. They don’t only make you look goodtaxalcool,blobo



Tester l’existence d’une variable en Python
le 9 avril , par sdf:

Still there is room for greater development and I would recommend more emphasis on research and of course it will bring a better situation. Thanks for sharing a nice read. Cheers The Krup Law Group



Tester l’existence d’une variable en Python
le 23 mars , par samdy:

There are many online stores having Cheap mbt footwear, i believe you gonna like it ! Yesterday, my friend bought a Mbt shoe for salewhich is so beautiful, i am surprised by the design and style.Do you have a pair of Mbt online now ? if not, go to online store and have one, it is so amazing !!!!!!



Tester l’existence d’une variable en Python
le 14 mars , par thomas sabo:

Thomas Sabo Necklaces not to mention cuff pendants can be from a lot of the single most basic type from necklaces utilising winter months summer, and there is no doubting who Thomas sabo Pendants could be maybe there most powerful form Thomas Sabo Charms of this X-mas.Seek multi-colored products which can end up bundled not to mention combined for just a wonderful form fact. for just a formalized Thomas Sabo Bracelet



In tiffanys jewellery online necklace store, You can chase after every kind of [Tiffany Co Jewelry->http://www.tiffanysilvers.co.uk/] you want, such as Silver Necklaces, Heart Necklaces, Beaded necklaces and Chain necklaces. The necklace styles are from classic to modern, and there must be one type to fit you.Tiffany jewelry based [Tiffany Necklace->http://www.tiffanysjewelry.co.uk/Necklace/index.html] on sale, classic or modern stylish quality jewelry for both women and men.-YCX
le 20 mai , par YCX:

In tiffanys jewellery online necklace store, You can chase after every kind of Tiffany Co Jewelry you want, such as Silver Necklaces, Heart Necklaces, Beaded necklaces and Chain necklaces. The necklace styles are from classic to modern, and there must be one type to fit you.Tiffany jewelry based Tiffany Necklace on sale, classic or modern stylish quality jewelry for both women and men.-YCX



Tester l’existence d’une variable en Python
le 27 avril , par max-k:

Et si tu veux tester l’existence de l’attribut d’une instance de classe (d’un objet) :

Exemple, pour l’instance en cours :

if hasattr(self, 'toto'):
   print(self.toto)
else:
   print("l'attribut toto n'est pas défini")

Exemple, pour un autre objet :

if hasattr(mon_objet, 'toto'):
   print(mon_objet.toto)
...


Tester l’existence d’une variable en Python
le 2 janvier , par David, biologeek:

Tu peux aussi faire : if ’toto’ in locals() : print toto




Webmaster Noplay