Linked Questions

13 questions linked to/from Determine if variable is defined in Python
53 votes
5 answers
86k views

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 "...
grigoryvp's user avatar
  • 43k
-3 votes
1 answer
3k views

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 ...
John Row's user avatar
1423 votes
16 answers
1.9m views

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

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 ...
19 votes
6 answers
29k views

How to do conditional compilation in Python ? Is it using DEF ?
18 votes
5 answers
3k views

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 ...
Mike Craig's user avatar
  • 1,717
18 votes
4 answers
6k views

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 ...
nemo's user avatar
  • 12.9k
7 votes
3 answers
30k views

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
Taras's user avatar
  • 509
5 votes
6 answers
741 views

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 ...
Conan's user avatar
  • 621
2 votes
1 answer
2k views

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? ...
Lin Ma's user avatar
  • 10.2k
0 votes
1 answer
2k views

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: ...
Shanoo's user avatar
  • 1,255
5 votes
2 answers
810 views

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, ...
javabrett's user avatar
  • 7,794
0 votes
2 answers
90 views

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("...