Linked Questions
13 questions linked to/from Determine if variable is defined in Python
53
votes
5
answers
86k
views
Easy way to check that a variable is defined in python? [duplicate]
Is there any way to check if a variable (class member or standalone) with specified name is defined? Example:
if "myVar" in myObject.__dict__ : # not an easy way
print myObject.myVar
else
print "...
-3
votes
1
answer
3k
views
How to make an "if" statement of when a variable is not defined? [duplicate]
For my code, I'm making an if statement typically an if statement that contains two if conditions. Look at the third line of code with hashtags beside it to understand what i'm asking
Look at the ...
1423
votes
16
answers
1.9m
views
How do I check if a variable exists?
I want to check if a variable exists. Now I'm doing something like this:
try:
myVar
except NameError:
# Do something.
Are there other ways without exceptions?
520
votes
34
answers
195k
views
When to throw an exception? [closed]
I have exceptions created for every condition that my application does not expect. UserNameNotValidException, PasswordNotCorrectException etc.
However I was told I should not create exceptions for ...
Community wiki
19
votes
6
answers
29k
views
Conditional compilation in Python
How to do conditional compilation in Python ?
Is it using DEF ?
18
votes
5
answers
3k
views
Python Exceptions: EAFP and What is Really Exceptional?
It's been said in a couple places (here and here) that Python's emphasis on "it's easier to ask for forgiveness than permission" (EAFP) should be tempered with the idea that exceptions should only be ...
18
votes
4
answers
6k
views
python closure with assigning outer variable inside inner function [duplicate]
I've got this piece of code:
#!/usr/bin/env python
def get_match():
cache=[]
def match(v):
if cache:
return cache
cache=[v]
return cache
return match
m = get_match()
m(1)
if ...
7
votes
3
answers
30k
views
How to check in python if variable is not None and greater than something in one line?
How make this statement in one line?
if x is not None:
if x > 0:
pass
if I write with just 'and' it shows exception if None
if x is not None and x > 0:
pass
5
votes
6
answers
741
views
Is there any expression in python that similar to ruby's ||=
I came across an interesting expression in Ruby:
a ||= "new"
It means that if a is not defined, the "new" value will be assigned to a; otherwise, a will be the same as it is. It is useful when doing ...
2
votes
1
answer
2k
views
for Python variable initialization
For Python variable (e.g. List and integer) if we do not initialize it, are they always None? Is there any scenario Python will do initialization for us even if we do not initialize it explicitly?
...
0
votes
1
answer
2k
views
Check if the dataframe exist, if so do merge
I have 3 dataframes (df1, df2, df3), out of which 'df3' might be created or not. If the dataframe df3 is created then merge all the three else just merge df1 & df2.
I am trying the below code:
...
5
votes
2
answers
810
views
Is it possible to test that test-dependencies have not leaked into real code with python/pytest
TL;DR: If I'm using pytest and some other test-only dependencies, is it possible to assert that none of these test-only dependencies have leaked into actual, non-test code?
In Java, when I run tests, ...
0
votes
2
answers
90
views
How do I find the user's name in a form?
So I am currently working on a Discord.py bot and am trying to find if the user is already registered through the bot with GSpread. I've tried a few things like :
if findUser is None:
print("...