473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Python's logical operators "sort circuit" execute

bartonc
6,596 Expert 4TB
By that I mean they stop evaluation at the first True part of the expression.
I use it often to replace an uninitialized object. Like this:
Expand|Select|Wrap|Line Numbers
  1. >>> aString = ""
  2. >>> aString or "Hello, world"
  3. 'Hello, world'
  4. >>> anObject = None
  5. >>> anObject or range(5)
  6. [0, 1, 2, 3, 4]
  7. >>> anObject = dict(a=1)
  8. >>> anObject or range(5)
  9. {'a': 1}
  10. >>> 
Jul 2 '07 #1
5 2022
Smygis
126 100+
This has other uses aswell.

Expand|Select|Wrap|Line Numbers
  1. >>> 1 == 2 and "True value" or "False value"
  2. 'False value'
  3. >>> 1 == 1 and "True value" or "False value"
  4. 'True value'
  5. >>> 1 == 1 and "" or "The 'True' value must evaluate to True" # or perhaps not?
  6. "The 'True' value must evaluate to True"
  7. >>> 1 == 2 and "The 'False' value does not." or ""
  8. ''
  9. >>> 
  10.  
Not that this is anything anyone uses often. But its nice to know.
Jul 2 '07 #2
bvdet
2,851 Expert Mod 2GB
It is sometimes useful for function defaults:
Expand|Select|Wrap|Line Numbers
  1. def polarEllipse(a, res, quad=1, xAxis=False, yAxis=False, rot=0):
  2.  
  3.     xAxis = float(xAxis or a.Ra)
  4.     yAxis = float(yAxis or a.p1.dist(a.p3))
  5.     ..........
Jul 3 '07 #3
Motoma
3,237 Expert 2GB
Good tip. Did you mean for this to be an Article?
Jul 3 '07 #4
bartonc
6,596 Expert 4TB
Good tip. Did you mean for this to be an Article?
Shhhh... That's my secret to high post counts: Publish in the forum, get lots of replies, then decide it should be an article and copy it there.
Jul 3 '07 #5
bartonc
6,596 Expert 4TB
It is sometimes useful for function defaults:
Expand|Select|Wrap|Line Numbers
  1. def polarEllipse(a, res, quad=1, xAxis=False, yAxis=False, rot=0):
  2.  
  3.     xAxis = float(xAxis or a.Ra)
  4.     yAxis = float(yAxis or a.p1.dist(a.p3))
  5.     ..........
Very useful!

I'm currently reading data from an RDBMS and storing to legacy text files (ongoing support for the predecessor of my project). The database may have empty fields, which are returned as None. The text file needs empty strings. So:
Expand|Select|Wrap|Line Numbers
  1. >>> field1 = None
  2. >>> field2 = None
  3. >>> ", ".join([str(field1 or ""), str(field2 or "")])
', '
Expand|Select|Wrap|Line Numbers
  1. >>> field2 = 7.6
  2. >>> ", ".join([str(field1 or ""), str(field2 or "")])
  3. ', 7.6'
  4. >>> 
Jul 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Arnaud | last post by:
Hi, I work with php 4.3.4 and I don't understand the way php sort this array for example : <? $array = (-1, 30, "test", true); sort($array); var_dump($array); ?>
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
0
by: anders thoresson | last post by:
Is it possible to resort the rows in a table like in a query where using ORDER BY, but have to new sort order stored in the table structure? -- anders thoresson -- MySQL General Mailing List...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
6
by: Rennie deGraaf | last post by:
In the last few days, I have discovered two "interesting" behaviours of the C language, both of which are apparently correct. Could someone please explain the reasoning behind them? 1. The...
2
by: Peter S. Guild | last post by:
Hello, I have a simple routine that uses a dataview to hold information. I need to sort these columns by section and then by desc. No matter what I do the dataview will not sort. What am I...
5
by: ano | last post by:
Hi, I have converted this VB code to C# but I got this error: "Operator '||' cannot be applied to operands of type 'int' and 'short'" Is VB allow to use Operator "Or" with 'int'? If yes, how...
6
by: Howie | last post by:
This must be a permissions issue. But I don't know how to fix it. I am in the middle of writing a database so I haven't even learned how to deal with security yet. Could someone tell me how to...
2
by: ronnysaid | last post by:
Hello, Is there a way to get Excel 2007 "sort by" value from a cell range?? to be more clear, if column header is country and you sorted cell range (A1,A10) by country, is there a way to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.