473,800 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python's logical operators "sort circuit" execute

bartonc
6,596 Recognized Expert Expert
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 2034
Smygis
126 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
Good tip. Did you mean for this to be an Article?
Jul 3 '07 #4
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Expert
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
2210
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
6584
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 FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
0
1481
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 For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=mysql@freebsd.csie.nctu.edu.tw
81
7358
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
6
1663
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 operators '^', '&', and '|' have lower precedance than '==', '!=', '>=", etc. I discovered this when the statement "if (array1 ^ array2 == 0xff)" failed to do what I expected. To me, it doesn't make any sense to give the bitwise operators lower...
2
7316
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 doing wrong? Language is C# ..Net framework is 1.1
5
6276
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 to do this in c#? Thanks,
6
1459
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 stop "sort ascending" in a table being greyed-out? Much appreciated. Thanks. -- Pontins History E-Mail: usenet@pontinshistory.co.uk Please visit www.pontinshistory.co.uk
2
1526
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 "country" using C#??? Thanks.
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.