Python IF THEN chain equivalence

Steve Holden steve at holdenweb.com
Sun Nov 16 20:17:14 EST 2008


jzakiya wrote:
> On Nov 13, 5:48 pm, "M.-A. Lemburg" <m... at egenix.com> wrote:
>> On 2008-11-13 23:31, jzakiya wrote:
>>
>>
>>
>>> On Nov 13, 5:21 pm, Alan Baljeu <alanbal... at yahoo.com> wrote:
>>>> I think you should rethink your post. The first case you posted makes no sense in any language I know.  Also, a whole lot of nested IF's is a bad idea in any language.  In Python, you will end up with code indented 40+ characters if you keep going.
>>>> ----- Original Message ----
>>>> From: jzakiya <jzak... at mail.com>
>>>> To: python-l... at python.org
>>>> Sent: Thursday, November 13, 2008 5:06:53 PM
>>>> Subject: Python IF THEN chain equivalence
>>>> I'm translating a program in Python that has this IF Then chain
>>>> IF  x1 < limit:   --- do a ---
>>>>     IF  x2 < limit:  --- do b ---
>>>>         IF x3 < limit:  --- do c ---
>>>>                        .-----
>>>>                         ------
>>>>                     IF  x10 < limt: --- do j ---
>>>>                     THEN
>>>>                  THEN
>>>>               -----
>>>>           THEN
>>>>      THEN
>>>> THEN
>>>> In other words, as long as    'xi' is less than 'limit' keep going
>>>> down the chain, and when 'xi' isn't less than 'limit' jump to end of
>>>> chain a continue.
>>>> Is this the equivalence in Python?
>>>> IF  x1 < limit:
>>>>         --- do a  ---
>>>> elif x2 < limit:
>>>>         --- do b ---
>>>> ----
>>>> ----
>>>> elif x10 < limit:
>>>>        --- do j ---
>>>> --http://mail.python.org/mailman/listinfo/python-list
>>>>       __________________________________________________________________
>>>> Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know athttp://ca.answers.yahoo.com
>>> In the code the 'xi's and 'limit' are variables and the --- do letters
>>> ---
>>> phrases are simply writes to any array:   an_array[xi]=0
>>> Actually, the code makes perfectly good sense, and is a necessity of
>>> the algorithm I'm implementing, and works perfectly good in Forth, and
>>> can be
>>> written quite nicely within a normal page width.
>>> I was just hoping I could perform the equivalent chain in Python
>>> without
>>> having to grossly indent the source code past the normal width of a
>>> printed page.
>>> But if that's the only way to do it in Python, then so be it.
>> You should probably consider using a function and then
>> convert the conditions to define return points:
>>
>> def do_something(...args...):
>>
>>     if x1 >= limit:
>>         return
>>     ...do a...
>>     if x2 >= limit:
>>         return
>>     ...do b...
>>     etc.
>>
>> That is provided I understand the flow of control in your
>> example... it's kind of strange to have THEN mark the *end*
>> of the then-branch in an if-then-else construct.
>>
>> --
>> Marc-Andre Lemburg
>> eGenix.com
>>
>> Professional Python Services directly from the Source  (#1, Nov 13 2008)>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>>>> mxODBC.Zope.Database.Adapter ...            http://zope.egenix.com/
>>>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
>> ________________________________________________________________________
>> 2008-11-12: Released mxODBC Connect 0.9.3      http://python.egenix.com/
>>
>> :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
>>
>>    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>>     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>>            Registered at Amtsgericht Duesseldorf: HRB 46611
> 
> It's interesting to see people think it's strange to have code that
> has multiple nested levels of IF THEN.  Apparently you haven't seen
> any Forth, assembly, et al code. All you're doing is having the branch
> point for each conditional be the end of the chain, otherwise it falls
> through to the code after the conditional.  This is done all the time
> in languages that let you actually manipulate the hardware.
> 
> Just as a suggestion :-)  a little humility would go a long way toward
> being open minded and receptive to different paradigms.  I've written
> this program I'm doing now in Python in 3 other languages (including
> Python, which I'm trying to make more efficient) and I seek to be
> flexible in my software linguistic capabilities.
> 
> I asked a very narrow question about a very specific language
> mechanism, and I know exactly what and why I'm doing what I'm doing.
> 
Well in my case what you are doing is pissing me off by failing to
explain your original requirements sufficiently well and then taking a
condescending attitude towards genuine efforts to help by people whose
reputations I know and respect.

One wonders why someone at your stratospheric ability level would even
need any help from the likes of us.

You talk about the need for humility: physician, heal thyself. However
good you may be at software, you demonstrate a marked lack of
interpersonal skills.

> I'll try some of the suggestions and see if they make the routine
> faster in Python.

That's a good idea.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list