![]() |
Python brevity
Obviously, some Python brevity is of the same type as the C/C++ ternary operator, which is to say, not really more efficient, but more efficient to write. How much do you use this in your Python? Here's an example:
:
for sets in re.finditer (pattern, doc, re.I): |
I think it depends on how easy it appears to read. As a general rule of thumb, I try to keep list comprehensions under 80 characters long.
|
Quote:
:
for sets in re.finditer(pattern, doc, re.I)::
def gather(pattern, doc): |
I'd design it with a slightly more general purpose "gather" function, myself:
:
def gather(sets, groups): |
Please explain to me why "is not" is safer practice....
|
I think it's just more human-readable than "not (x is y)". I don't believe it has any other significance.
|
Well, I can easily accept that. I don't know from generators, I'll have to put that in lesson 2.
|
:
if found is not None:
if found != NoneI am a total Python noob. That means ignorant, or uninformed; not stupid. Please do not mislead me while I'm trying to learn, whether it's to promote yourself as a guru, or high-priest, or for any other reason. If you care to give a rational dissertation on why one of the above is safer than the other (particularly in this circumstance), please feel free to do so. I'll certainly read it with all my attention. |
Quote:
I thought you were asking why "x is not y" is used instead of "not (x is y)", a question that I've pondered before (because whilst "is not" makes sense in English, it makes less sense from a programming perspective). I didn't realise you were asking the difference between "==" and "is". As for why "is" should be used instead of "==" for checking None, well, in most cases, it doesn't matter. However, Python objects can overide certain operators (as in C++), thus it's possible that an object will tell you that it is equal to None, without actually being None. Using "is" ensures that the object really is (or is not) None in all circumstances. |
It wasn't directed at you, Arevos, it was directed at the rewriting of my code from
:
medList.append ([found for found in sets.group (1, 2, 3) if found != None]):
if found is not None] # "is not" is safer practice in general |
| All times are GMT -5. The time now is 4:54 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC