![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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):
medList.append ([found for found in sets.group (1, 2, 3) if found != None])
tagList.append (medList)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
for sets in re.finditer(pattern, doc, re.I):
matches = [found for found in sets.group (1, 2, 3)
if found is not None] # "is not" is safer practice in general
medList.append (matches)
tagList.append (medList) def gather(pattern, doc):
yield [[found for found in sets.group (1, 2, 3)
if found is not None]
for sets in re.finditer(pattern, doc, re.I)]
def run():
taglist = [gather(pattern, doc)
for pattern, doc in fetchlist()]
use(taglist) |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I'd design it with a slightly more general purpose "gather" function, myself:
def gather(sets, groups):
for found in sets.group(*groups):
if found is not None: yield
found = re.finditer (pattern, doc, re.I)
medList = [x for x in gather(sets, (1, 2, 3)) for sets in found] |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Please explain to me why "is not" is safer practice....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I think it's just more human-readable than "not (x is y)". I don't believe it has any other significance.
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, I can easily accept that. I don't know from generators, I'll have to put that in lesson 2.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
if found is not None if found != None I 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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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. |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|