Like RoundTower mentioned, in that specific case you should probably just do:
Quote:
v = dic.get(keyVal, -1)
In the more general case, I kind of agree that I've never felt that easy with the really common use of exceptions. On one hand I've had the "only use exceptions for truly exceptional situations" mantra drilled into my head for awhile - and generally agree with it. On the other hand, its handy to not have a lot of special in-band return values that need to be checked for/handled.
I'm coming from using C++. The last formal instruction I had was in college. I was using Dev-C++ as my programming kit/compiler. So I'm a little lost as to how exactly Python works as a scripting language. Is there a front-end that I'll be working with?
You do not need IDE. In case you need to debug you can use print function. This by far best and superior way for writing programs.
For simple editor you can use IDLE (it comes whit python distribution), but if you really think that you can not live without VisualStudio look like software, debugger and code completion try using Eclipse whit Pydev plugin.
You do not need IDE. In case you need to debug you can use print function. This by far best and superior way for writing programs.
Bash IDEs all you want but please, please learn there's more to debugging than print statements. Sometimes a full debugger is overkill... but sometimes a debugger is the only way.
Bash IDEs all you want but please, please learn there's more to debugging than print statements. Sometimes a full debugger is overkill... but sometimes a debugger is the only way.
Indeed. If someone told me that they never used a debugger... I wouldn't work with that person.
And this is coming from someone that uses Print statements way too often.
You do not need IDE. In case you need to debug you can use print function. This by far best and superior way for writing programs.
For simple editor you can use IDLE (it comes whit python distribution), but if you really think that you can not live without VisualStudio look like software, debugger and code completion try using Eclipse whit Pydev plugin.
What the hell? Isn't half the reason why python is awesome is the PDB module and the add-ons you can use with it? Do you really just use print statements over PDB? Really?
You do not need IDE. In case you need to debug you can use print function. This by far best and superior way for writing programs.
Relying on print statements for debugging anything of increasing complexity can become a bad habit, according to what I've learned.
I do agree that an IDE is not critical though, but it can be convenient. I prefer Emacs, but I also use an IDE too.
Wing IDE is a Python specific IDE that's free for non-commercial purposes. It doesn't have any overhead to setting it up (unlike Eclipse), so it can be good for beginners. And Eclipse has already been mentioned.
ok am i losing my mind, or is this behavior just absurdly retarded?
(yes, i appreciate the irony of my printf debugging considering the above discussion, but i'm trying to demonstrate a point.)
Code:
#!/usr/bin/env python
list = [1, 'a', 'b', 'c', 4]
for item in list:
print "list before: %s" % list
try:
item = int(item)
except ValueError:
print "%s cannot be cast to int. Delete it." % item
list.remove(item)
else:
print "%s looks like an int. No problem, mon." % item
print "list after : %s" % list
print
print "### final list is %s" % list
seems straightforward to me. however...
Spoiler:
Code:
list before: [1, 'a', 'b', 'c', 4]
1 looks like an int. No problem, mon.
list after : [1, 'a', 'b', 'c', 4]
list before: [1, 'a', 'b', 'c', 4]
a cannot be cast to int. Delete it.
list after : [1, 'b', 'c', 4]
list before: [1, 'b', 'c', 4]
c cannot be cast to int. Delete it.
list after : [1, 'b', 4]
### final list is [1, 'b', 4]
in other words, when i delete 'a' from list, the for loop doesn't notice and ends up skipping the next entry (since it moved back a slot after i deleted 'a'). i can draw a pointer diagram of what i think is happening if that will help .
is there some simple workaround to this problem? do i need to make a separate clone of list for purposes of iterating? ugh, python.
i mean, sure, i guess. i guess i don't know enough about how python handles its iterators internally to have an expectation that this would work.
that said, i feel like this pattern works fine in other languages. maybe i'm just losing my mind, as alluded to in my previous post .
edit: adding list_copy = copy.deepcopy(list) and iterating on that seems to solve the problem. i think the real solution is to use map/apply/whatever-the-****-python-calls-it with a lambda or small function, but i don't think it will greatly improve efficiency and it will probably hurt readability.