Timeline for Determine if variable is defined in Python [duplicate]
Current License: CC BY-SA 3.0
27 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 31, 2020 at 6:45 | history | closed | samliew | Duplicate of Easy way to check that a variable is defined in python? [duplicate] | |
| S Jan 31, 2020 at 6:45 | history | edited | Community Bot |
remove duplicate link
|
|
| S Jan 31, 2020 at 6:45 | history | reopened | samliew | ||
| Jan 31, 2020 at 6:45 | history | closed | samliew | Duplicate of Easy way to check that a variable is defined in python? [duplicate] | |
| Jan 31, 2020 at 6:45 | history | reopened | samliew | ||
| Apr 15, 2018 at 17:08 | comment | added | Bachsau |
@0xC0000022L The import machinery is heavy. And it is not intended to be used for "configuration files". If thats the only thing you want, you'd be better of using exec or eval. However, that would be very insecure in many ways, so I think its worth a simple parser. Anyway, the nearest equivalent to an isset() would be hasattr(). One could also use globals().get() which is capable of returning a default.
|
|
| Jan 22, 2018 at 16:34 | comment | added | r2evans |
@dangel, yes. You may think "that variable will never be referenced if not condition", however it has been shown time and again (personal experience, at least) that programmers are imperfect and logic flows can in fact be different than expectations. This is especially true in spiral-development, where assumptions change and not all code is audited/refactored. It costs "almost nothing" to set the variable regardless of condition, even if only used when condition is met.
|
|
| Dec 10, 2017 at 3:07 | comment | added | dangelsaurus | dumb question here, I know I'm overthinking this... By "conditionally setting a variable", are you saying the existence of a variable shouldn't be based on a condition? | |
| May 23, 2017 at 12:26 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
| Jul 20, 2016 at 9:49 | comment | added | MarkJL | @S.Lott: What about in a memoizing method? You can return or set conditionally. It's a std in ruby, that way of thinking is why I am here. | |
| S Sep 19, 2012 at 2:27 | history | edited | Community Bot |
insert duplicate link
|
|
| S Sep 19, 2012 at 2:27 | history | closed |
DNA mmmmmm ЯegDwight Peter Brown joran |
exact duplicate | |
| Sep 18, 2012 at 9:15 | review | Close votes | |||
| Sep 19, 2012 at 2:27 | |||||
| Mar 22, 2012 at 11:29 | comment | added | S.Lott | @STATUS_ACCESS_DENIED: That's what defaults are appropriate. First set the defaults. Then import your configuration. Now all variables are defined, either as defaults or overrides in the configuration file. You can easily avoid conditionally setting a variable. | |
| Mar 21, 2012 at 0:51 | comment | added | 0xC0000022L |
@S.Lott: I have to disagree. When I use import to "source" a "config file" (i.e. a file that only has assignments in it), it may very well be that some variable has not been defined there. And import is sure cheaper/simpler than ConfigParser, so for me pragmatism wins over beauty here.
|
|
| Mar 3, 2012 at 22:55 | history | edited | user102008 | CC BY-SA 3.0 |
added 26 characters in body
|
| Dec 28, 2011 at 14:20 | answer | added | xsreality | timeline score: 5 | |
| Mar 31, 2010 at 13:48 | comment | added | Bite code | +1 to S.Lott anwser. The fact that there is a way to do it doesn't mean you should use it in a fresh project. | |
| Oct 20, 2009 at 10:35 | comment | added | S.Lott | Please, please, please, do not "conditionally" set a variable. That's just dreadful, horrible design. Always, always, always provide for all logic paths. Please do not continue doing this. | |
| Oct 20, 2009 at 8:19 | vote | accept | user102008 | ||
| Oct 20, 2009 at 6:50 | comment | added | carl | Duplicates: stackoverflow.com/questions/843277/…, stackoverflow.com/questions/750298/…, | |
| Oct 20, 2009 at 6:30 | answer | added | Denis Otkidach | timeline score: 6 | |
| Oct 20, 2009 at 5:15 | answer | added | divegeek | timeline score: 180 | |
| Oct 20, 2009 at 5:08 | answer | added | John La Rooy | timeline score: 499 | |
| Oct 20, 2009 at 5:07 | answer | added | Alex Martelli | timeline score: 813 | |
| Oct 20, 2009 at 5:06 | answer | added | sc45 | timeline score: 27 | |
| Oct 20, 2009 at 5:03 | history | asked | user102008 | CC BY-SA 2.5 |