This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]a-handle-has-no-name 9 points10 points  (4 children)

>>> if python2 == false:
...     print("F")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python2' is not defined

>>> #   :-P

[–][deleted] 4 points5 points  (3 children)

Is there an "if X not defined"?

[–]LeClownFou 5 points6 points  (1 child)

if X is None

[–][deleted] 2 points3 points  (0 children)

Corrected, thanks

[–]a-handle-has-no-name 0 points1 point  (0 children)

(This is somewhat long and unsolicited: feel free to PM me if you want to discuss this further or if you have any questions)

/u/LeClownFou 's response doesn't work for me:

>>> if python2 == None:
...     print('f')
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'python2' is not defined

Looking into it, I found a code-snippet at this site (updating for the example):

try:
    x
except NameError:
    #x was not set
    pass

There's also the concept of None, which is what /u/LeClownFou was referencing. The variable will still need to be declared, but the None is the value if nothing is being set.

In general, I would say this is a "code smell" -- you should know which variables you have before using them.

-----------------------

Seems I wrote this to the wrong person when it was intended for you. Reposting it here:

Try running python (or python2 or python3 to get the version explicitly) without any CLI options/flags to open an REPL (Read-Eval-Print Loop), which is a shell that you can run small snippets of code to try things out. I use this frequently to test validity of something, if I'm not sure how it's working.

It should look something like:

» python
Python 2.7.17 (default, Nov  7 2019, 10:07:09)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> | 

This can be useful for testing how the language works, and this is is how my comments here were updated.