0

While writing state machines to analyze different types of text data, independent of language used (VBA to process .xls contents using arrays/dictionaries or PHP/Python to make SQL insert queries out of .csv's) I often ran into neccesity of something like

boolean = False
while %sample statement%:
    x = 'many different things'
    if boolean == False:
        boolean = True
    else:
        %action that DOES depend on contents of x
             that need to do every BUT first time I get to it%

Every time I have to use a construction like this, I can't help feeling noob. Dear algorithmic gurus, can you assure me that it's the only way out and there is nothing more elegant? Any way to specify that some statement should be "burnt after reading"? So that some stupid boolean is not going to be checked each iteration of the loop

mekkanizer
  • 722
  • 2
  • 9
  • 28
  • Maybe see [this](http://stackoverflow.com/questions/27626917/how-to-tell-when-a-method-is-called-for-first-time-of-many) (It's not quite the same as your question. There are probably others out there closer.) – dwn Feb 10 '15 at 18:21
  • @dwn things discussed there are obvious to me and, unfortunately, **NOT** helping at all. I'll edit my question now to point out that the action on the last line **depends** on preceeding code inside the loop – mekkanizer Feb 10 '15 at 18:26
  • (OT) Read the title as "Python - disposable lies". A pretty sinister one! – ivan_pozdeev Feb 11 '15 at 01:10
  • OTOH, these awkward constructs do look like "lies" (i.e. feel very wrong) – ivan_pozdeev Feb 11 '15 at 01:14

2 Answers2

1

The only things that come across as slightly "noob" about this style are:

  • Comparing a boolean variable to True or False. Just write if <var> or if not <var>. (I'll ignore the = vs == as a typo!)
  • Not giving the boolean variable a good name. I know that here boolean is just a placeholder name, but in general using a name like first_item_seen rather than something generic can make the code a lot more readable:

    first_item_seen = False
    while [...]:
        [...]
        if first_item_seen:
            [...]
        else:
            first_item_seen = True
    

Another suggestion that can work in some circumstances is to base the decision on another variable that naturally conveys the same state. For instance, it's relatively common to have a variable that contains None for the first iteration, but contains a value for later iterations (e.g. the result so far); using this can make the code slightly more efficient and often slightly clearer.

psmears
  • 26,070
  • 4
  • 40
  • 48
  • I firstly didnt think about asking *specifically* about python, wrote "pseudocode" that didnt belong to any specific language, and thats why I named boolean variable "boolean" and used a = instead of ==. – mekkanizer Feb 10 '15 at 18:33
  • Of course, one other alternative is to define functions f0() and f1(), and then write ... f0() while [...]: x=BLAH f0() f1(x) – dwn Feb 10 '15 at 18:34
  • Thanks for the idea with checking some other related things, it's not universal, but is *actually more elegant* than using an extra boolean. If nothing better is suggested, I guess I'll acept that – mekkanizer Feb 10 '15 at 18:35
  • @dwn Sorry, I did not get your point at all. What's that sample code you wrote? It doesn't explain anything to me! How will that *simplify* anything? – mekkanizer Feb 10 '15 at 18:38
  • @mekkanizer Depends on the actual code if it will simplify anything. It would at least not require a boolean, "So that some stupid boolean is not going to be checked each iteration of the loop". – dwn Feb 10 '15 at 18:44
  • @mekkanizer Basically I'm just saying one option is to break it into two functions, and run one before the loop – dwn Feb 10 '15 at 18:50
  • @dwn I see what you are saying. HOW running something before the loop can help me if the things I want to do are related to content being defined/changed INSIDE the loop? There is just no sense in doing some extra actions before the loop, it's not simplifying, it's making more complex! – mekkanizer Feb 10 '15 at 19:00
  • @mekkanizer You can pass your variable into the function. I didn't say it simplified it; I have no idea what your code is. – dwn Feb 10 '15 at 19:06
  • @dwn What a strange suggestion even NOT regarding the code. Look. X is passed into function every iteration of the loop and some decision is made over there **or** some decision is made inside the loop. Passing it into function only makes things more complex. Functions are generally used to code some repeating pattern once. Here I see no reasons to use functions (pls tell me at least one?), I only see reasons *against*. – mekkanizer Feb 10 '15 at 19:15
0

If I understand your problem correctly, I'd try something like

x = 'many different things'
while %sample statements%:
    x = 'many different things'
    action_that_depends_on_x()

It is almost equivalent; the only difference is that in your version the loop body could be never executed (hence x never being computed, hence no side effects of computing x), in my version it is always computed at least once.

user58697
  • 7,808
  • 1
  • 14
  • 28
  • No. You got me wrong. Again. I compute contents of `x` inside loop and need to operate them *inside* loop, every BUT first time I get to the %use_x_for_some_action% part – mekkanizer Feb 11 '15 at 06:13