Re: Good code patterns in Python

Discussion in 'Python' started by Bob Gailer, Jul 1, 2003.

  1. Bob Gailer

    Bob Gailer Guest


    >Will Stuyvesant writes:
    > > If you know that your source code is going to be used
    > > later by others, then I feel that code with the pattern:
    > >
    > > if some_condition:
    > > some_name = some_value
    > > else:
    > > some_name = other_value
    > >
    > > is often a mistake. Much better, safer, would be:
    > >
    > > some_name = some_value
    > > if not some_condition:
    > > some_name = other_value


    This brings back the good old days of FORTRAN IV which had a
    single-statement IF and no ELSE. Thus:
    C = VALUE1
    IF ( A .EQ. B) C = VALUE2
    Notice the indentation. Cols 1-5 were reserved for line # and col 6 for the
    continuation code. So Python is not the only indentation dependent
    language. Nor is it the first to use indentation to convey structure.

    Bob Gailer

    303 442 2625


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003
    Bob Gailer, Jul 1, 2003
    #1
    1. Advertising

  2. Collective memory (was: Good code patterns in Python)

    In article <>,
    Bob Gailer <> wrote:
    .
    .
    .
    >This brings back the good old days of FORTRAN IV which had a
    >single-statement IF and no ELSE. Thus:
    > C = VALUE1
    > IF ( A .EQ. B) C = VALUE2
    >Notice the indentation. Cols 1-5 were reserved for line # and col 6 for the
    >continuation code. So Python is not the only indentation dependent
    >language. Nor is it the first to use indentation to convey structure.

    .
    .
    .
    Maybe youngsters don't realize this.

    One monotonous but unremitting argument that Python inspires
    is about the wisdom of its white-space-significance (WSS).
    Modern programmers might not realize how strongly old-timers
    associate WSS with early FORTRAN and COBOL (as we capitalized
    them then), and how unpleasant some of those memories are.
    *That*, I assume, is why WSS discussions become as heated as
    they do.
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Personal: http://phaseit.net/claird/home.html
    Cameron Laird, Jul 3, 2003
    #2
    1. Advertising

  3. Bob Gailer

    Pete Fenelon Guest

    Re: Collective memory

    In alt.folklore.computers Rupert Pigott <> wrote:
    >
    > OCCAM used WS to denote block structure... For


    ....and was generally written with folding editors that just "got it
    right" ;)

    pete
    --
    "there's no room for enigmas in built-up areas" HMHB
    Pete Fenelon, Jul 3, 2003
    #3
  4. Re: Collective memory

    "Pete Fenelon" <> wrote in message
    news:...
    > In alt.folklore.computers Rupert Pigott

    <> wrote:
    > >
    > > OCCAM used WS to denote block structure... For

    >
    > ...and was generally written with folding editors that just "got it
    > right" ;)


    I noticed that a few of the KDE 3.1 editors support folding... :)

    Took me a while to work out what those lines & + things were in
    the left margin... Made me happy. :)

    Cheers,
    Ruppert
    Rupert Pigott, Jul 3, 2003
    #4
  5. Bob Gailer

    Pete Fenelon Guest

    Re: Collective memory

    In alt.folklore.computers Rupert Pigott <> wrote:
    >
    > I noticed that a few of the KDE 3.1 editors support folding... :)


    Hmmmm, editor and GUI are two concepts that I try to keep firmly
    separated ;)

    pete
    --
    "there's no room for enigmas in built-up areas" HMHB
    Pete Fenelon, Jul 3, 2003
    #5
  6. Re: Collective memory (was: Good code patterns in Python)

    In article <>, Cameron Laird wrote:

    > Maybe youngsters don't realize this.
    >
    > One monotonous but unremitting argument that Python inspires
    > is about the wisdom of its white-space-significance (WSS).
    > Modern programmers might not realize how strongly old-timers
    > associate WSS with early FORTRAN and COBOL (as we capitalized
    > them then), and how unpleasant some of those memories are.
    > *That*, I assume, is why WSS discussions become as heated as
    > they do.



    The problem with Python is that its use of indentation is not the same
    as COBOL or FORTRAN. Apples and Oranges.

    Those languages just had an arbitrary format, but it was specific and
    easy to catch.

    Python uses it for actually determining the logic in your program, which
    IMHO is dangerous.

    if <something>
    <work>
    <more work>
    <add numbers>

    Is <add numbers> part of the if statement, or did someone with different
    tab settings from the author make a mistake and align it accidentally?

    One really funny problem was when I guy reformatted his Python code,
    around 15K lines of it, and basically unindented *ALL* of the code to
    column 1. It was the only recoverable copy of the code too.

    He had to read the entire program line by line to recreate the logic.

    The thing that bothers me though is I have found indent-related bugs in
    Python code that no one knew about. The code still ran, but some people
    had obviously made some bad assumptions about the logic and indented
    it wrong, changing the nature of the program.

    I know a lot of people disagree, but I really think whitespace should
    not determine flow-control in a program.


    --
    Ah... you gotta love it when your ISP switches to a SPAMMING newsfeed.
    Sigh...



    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    Charles Shannon Hendrix, Jul 5, 2003
    #6
  7. Re: Collective memory

    Charles Shannon Hendrix wrote:
    > Python uses it for actually determining the logic in your program, which
    > IMHO is dangerous.
    >
    > if <something>
    > <work>
    > <more work>
    > <add numbers>
    >
    > Is <add numbers> part of the if statement, or did someone with different
    > tab settings from the author make a mistake and align it accidentally?


    To catch such mistakes, Python has the -tt command line option:
    -t : issue warnings about inconsistent tab usage (-tt: issue errors)

    There's also tabnanny.py in the standard library to check things
    before executing code. I'm not sure, but I suspect that PyChecker
    also checks this.


    --Irmen
    Irmen de Jong, Jul 5, 2003
    #7
  8. Re: Collective memory (was: Good code patterns in Python)

    "Charles Shannon Hendrix" <> wrote in message
    news:...
    > In article <>, Cameron Laird wrote:
    >

    [..snip...]
    > One really funny problem was when I guy reformatted his Python code,
    > around 15K lines of it, and basically unindented *ALL* of the code to
    > column 1. It was the only recoverable copy of the code too.
    >
    > He had to read the entire program line by line to recreate the logic.

    What happens if you replace brackets { with '' in C/C++. What you are
    talking about is being bitten by a lack of version control not a problem
    with the language, imho. What if this same programmer removed all asterisks
    from some c source? As mentioned in an earlier post, tabnanny. I knew a
    guy, who in his first couple of weeks as a programmer, was cleaning up some
    testing folders and managed to rm * the development source. After soiling
    myself and taking a deep breath, I restored the files from backup. <g> Two
    lessons to be learned: 1) It wasn't rm's fault nor the fault of my source
    code, it was my own brain-dead action. 2) That which does not kill us, makes
    us stronger.

    -jeff
    Jeff Hinrichs, Jul 6, 2003
    #8
  9. Re: Collective memory

    Irmen de Jong <> wrote:

    >Charles Shannon Hendrix wrote:
    > > Python uses it for actually determining the logic in your program, which
    > > IMHO is dangerous.


    Not at all. It just means that I will never use Python if it is
    at all avoidable. My backhanded compliments: it is actually less safe
    than C.

    > > if <something>
    > > <work>
    > > <more work>
    > > <add numbers>
    > >
    > > Is <add numbers> part of the if statement, or did someone with different
    > > tab settings from the author make a mistake and align it accidentally?

    >
    >To catch such mistakes, Python has the -tt command line option:
    >-t : issue warnings about inconsistent tab usage (-tt: issue errors)


    What is inconsistent about the tab usage above? That is, why
    would it be flagged?

    >There's also tabnanny.py in the standard library to check things
    >before executing code. I'm not sure, but I suspect that PyChecker
    >also checks this.


    Sincerely,

    Gene Wirchenko

    Computerese Irregular Verb Conjugation:
    I have preferences.
    You have biases.
    He/She has prejudices.
    Gene Wirchenko, Jul 6, 2003
    #9
  10. Re: Collective memory (was: Good code patterns in Python)

    On Fri, Jul 04, 2003 at 11:08:33PM -0400, Charles Shannon Hendrix wrote:
    >
    > Python uses it for actually determining the logic in your program, which
    > IMHO is dangerous.
    >
    > if <something>
    > <work>
    > <more work>
    > <add numbers>
    >
    > Is <add numbers> part of the if statement, or did someone with different
    > tab settings from the author make a mistake and align it accidentally?


    C code is indented to reflect its structure, but uses braces to reflect that
    structure. This is IMHO worse.

    if (something)
    work();
    more_work();
    add_numbers();

    Is add_numbers() part of the if statement? No. Did the author intend it to
    be? Maybe. You can't tell. They've indented it to imply one way, but it
    actually works another way.

    Given that you should indent your code to reflect the structure anyway,
    Python simply discards the redundant punctuation, and avoids this sort of
    ambiguity. "There should be one-- and preferably only one --obvious way to
    do it."

    > One really funny problem was when I guy reformatted his Python code,
    > around 15K lines of it, and basically unindented *ALL* of the code to
    > column 1. It was the only recoverable copy of the code too.
    >
    > He had to read the entire program line by line to recreate the logic.


    "Doctor, when I randomly corrupt my source, it doesn't work anymore!"

    "So don't do that then."

    As another poster pointed out, deleting e.g. all the asterisks from C code
    would be equally painful. It's not Python's fault that guy wasn't using a
    version control system.

    -Andrew.
    Andrew Bennetts, Jul 6, 2003
    #10
  11. Re: Collective memory

    Charles Shannon Hendrix wrote:


    > The problem with Python is that its use of indentation is not the same
    > as COBOL or FORTRAN. Apples and Oranges.
    >
    > Python uses it for actually determining the logic in your program, which
    > IMHO is dangerous.
    >
    > if <something>
    > <work>
    > <more work>
    > <add numbers>
    >
    > Is <add numbers> part of the if statement, or did someone with different
    > tab settings from the author make a mistake and align it accidentally?

    Well, no matter WHAT the language, I find tabs are evil little things
    that introduce special cases and the need for code to handle them.
    That said though, I find python block structure (sans tabs), just fine
    thanks.
    It would be nice if tabs as indentation for Python was deprecated but
    failing that, If you don't use tabs, its wonderful.

    If you do try python, try not to indent with tabs, and please try it, I
    did and look at me now :)

    Cheers, Pad.
    Donald 'Paddy' McCarthy, Jul 6, 2003
    #11
  12. Bob Gailer

    Alan Kennedy Guest

    Re: Collective memory (was: Good code patterns in Python)

    Andrew Bennetts wrote:

    > C code is indented to reflect its structure, but uses braces to reflect
    > that structure. This is IMHO worse.
    >
    > if (something)
    > work();
    > more_work();
    > add_numbers();


    Back in my C programming days (late 80s, 90s), I used to always indent
    my code python style anyway (before I even knew of the existence of
    python, ABC, etc). For example

    if (cond)
    {
    work();
    more_work();
    }
    else
    {
    other_work();
    and_yet_more();
    }

    And I still write all my java that way to this day.

    I occasionally got and get some complaints/ribbing from co-workers,
    who most often preferred a style like this :-

    if (cond) {
    work();
    more_work();
    } else {
    otherwork();
    }

    But to me that was far less readable, especially when there are
    multiple nested if/switch/while/etc statements. OK, you can find out
    out where code blocks begin and end by using the bracket matching
    facilities available in most editors, but that's no use to you when
    you're reading a hard-copy printout, for example.

    And in practically every C job I ever did, I used to get compliments
    from "occasional programmers" (e.g. hardware engineers), who sometimes
    had to work with my code to make minor changes. They would say things
    like "your code is so easy to read", "I was able to find really
    quickly the bit that I needed to change", etc. It was only the full
    time programmers who complained, and they were generally from the "it
    was hard to write, it should be hard to read" camp.

    When I found python (oh serendipity :), the indentation was
    completely natural to me, and I got it right from my very first if
    statement. It made my source look more readable, since it did away
    with all those lines containing only '{' and '}'.

    born-to-code-python-style-ly yrs :)

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
    Alan Kennedy, Jul 6, 2003
    #12
  13. Re: Collective memory

    I keep hitting the wrong key when following up... :(

    In article <>, Donald 'Paddy'
    McCarthy wrote:

    >> Is <add numbers> part of the if statement, or did someone with different
    >> tab settings from the author make a mistake and align it accidentally?


    > It would be nice if tabs as indentation for Python was deprecated but
    > failing that, If you don't use tabs, its wonderful.


    That would make it even worse.

    > If you do try python, try not to indent with tabs, and please try it, I
    > did and look at me now :)


    Its precisely because I used it at work that I don't use it now...

    There is a lot to like about it, but there's never been a compelling
    reason to use it. We used it because it was buzzword compliant at the
    time.



    --
    Ah... you gotta love it when your ISP switches to a SPAMMING newsfeed.
    Sigh...



    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    Charles Shannon Hendrix, Jul 7, 2003
    #13
  14. Bob Gailer

    John J. Lee Guest

    Re: Collective memory

    Charles Shannon Hendrix <> writes:

    > In article
    > <>, Ben
    > Hutchings wrote:

    [...]
    > I debugged and maintained big Python programs, and it was a frequent
    > error and problem.

    [...]

    So, given that people here *have* maintained big Python programs, and
    haven't had these problems, what do you conclude about your team's
    development practices?


    John
    John J. Lee, Jul 7, 2003
    #14
  15. Bob Gailer

    Kevin Handy Guest

    Re: Collective memory (was: Good code patterns in Python)

    Cameron Laird wrote:
    > In article <>,
    > Bob Gailer <> wrote:
    > .
    > .
    > .
    >
    >>This brings back the good old days of FORTRAN IV which had a
    >>single-statement IF and no ELSE. Thus:
    >> C = VALUE1
    >> IF ( A .EQ. B) C = VALUE2
    >>Notice the indentation. Cols 1-5 were reserved for line # and col 6 for the
    >>continuation code. So Python is not the only indentation dependent
    >>language. Nor is it the first to use indentation to convey structure.

    >

    FORTRAN isn't indentation dependent, it is position dependent.
    There is a big difference between the two.

    In FORTRAN, certial columns were allocated to specific purposes.
    1-5 for the line number, 6 for comment/continuation, 7-75 for the
    program line, 76-80 for sequence numbers. [iirc]

    If you want a language that was even more position dependent than
    fortran, look at RPG-II. You used printed forms for programming to
    get the columns correct. One character in the wrong column would
    change the meaning of that line.

    In PYTHON, it is the indentation itself that matters, not the
    actual column position.

    > .
    > .
    > .
    > Maybe youngsters don't realize this.
    >
    > One monotonous but unremitting argument that Python inspires
    > is about the wisdom of its white-space-significance (WSS).
    > Modern programmers might not realize how strongly old-timers
    > associate WSS with early FORTRAN and COBOL (as we capitalized
    > them then), and how unpleasant some of those memories are.
    > *That*, I assume, is why WSS discussions become as heated as
    > they do.
    Kevin Handy, Jul 7, 2003
    #15
  16. Bob Gailer

    Guest

    Re: Collective memory

    In article <>,
    (Tim Shoppa) wrote:
    <snip>

    >Remember the Dilbert where PHB complains that his programmers are using
    >way too many semicolons? :)


    All RIGHT! That's a good PHB. It was so difficult to distinguish
    between a semicolon and a colon on the listings.

    /BAH

    Subtract a hundred and four for e-mail.
    , Jul 8, 2003
    #16
  17. Bob Gailer

    Tim Shoppa Guest

    Re: Collective memory

    Donald 'Paddy' McCarthy <> wrote in message news:<>...
    > Charles Shannon Hendrix wrote:
    >
    >
    > > The problem with Python is that its use of indentation is not the same
    > > as COBOL or FORTRAN. Apples and Oranges.


    And it's not the same as Makefiles, either. Everybody has at one time
    been bitten by how 8 spaces is not at all the same in a Makefile as
    one tab; and it doesn't help that with many editing/viewing tools that it
    is impossible to see the difference.

    > > Python uses it for actually determining the logic in your program, which
    > > IMHO is dangerous.


    Again, it is certainly no use than Makefiles.

    > Well, no matter WHAT the language, I find tabs are evil little things
    > that introduce special cases and the need for code to handle them.


    Compare that to all the punctuation marks that *some* other languages require.
    Remember the Dilbert where PHB complains that his programmers are using
    way too many semicolons? :)

    Tim.
    Tim Shoppa, Jul 8, 2003
    #17
  18. Re: Collective memory (was: Good code patterns in Python)

    Kevin Handy wrote:
    >
    > [snip...] [snip...] [snip...]
    >
    > FORTRAN isn't indentation dependent, it is position dependent.
    > There is a big difference between the two.
    >
    > In FORTRAN, certial columns were allocated to specific purposes.
    > 1-5 for the line number, 6 for comment/continuation, 7-75 for the
    > program line, 76-80 for sequence numbers. [iirc]
    >
    > If you want a language that was even more position dependent than
    > fortran, look at RPG-II. You used printed forms for programming to
    > get the columns correct. One character in the wrong column would
    > change the meaning of that line.
    >

    IIRC, people who programmed RPG-II on a computer terminal...had
    a plastic overlay for the screen so they could see what column
    that things were being typed into.

    The "C" language gives white space significance...especially
    if it determines what a token will be. The tokenizer tries to
    build the longest token possible. So:

    x = i++ + j;

    is *not* the same as:

    x = i + ++j;

    and the statement:

    x = i+++j;

    is the same as:

    x = i++ + j;

    If you have:

    int *p, x, y;

    then:

    y = x/*p;

    is quite different from:

    y = x / *p;

    The first way, "/*" will begin a comment...the second way,
    you get the integer "x" divided by the integer pointed to by "p".


    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
    Charles Richmond, Jul 8, 2003
    #18
  19. Re: Collective memory

    In article <>, Tim Shoppa wrote:

    > And it's not the same as Makefiles, either. Everybody has at one time
    > been bitten by how 8 spaces is not at all the same in a Makefile as
    > one tab; and it doesn't help that with many editing/viewing tools that it
    > is impossible to see the difference.


    Listing one bad thing to defend another?

    I hate that part of make too.

    However, its very easy to tab a makefile properly, because the logic
    is implicit in the rule sets. You just find each rule and indent its
    members properly. It's not ambiguous.

    You also get an error... in Python, you'll just have a different flow of
    logic, which may or may not show errors.

    As long as you are strict and careful, Python is OK, but I will likely
    never like invisible flow-control characters. In makefiles either... :)

    >> > Python uses it for actually determining the logic in your program, which
    >> > IMHO is dangerous.

    >
    > Again, it is certainly no use than Makefiles.


    Heh heh... I agree...

    >> Well, no matter WHAT the language, I find tabs are evil little things
    >> that introduce special cases and the need for code to handle them.

    >
    > Compare that to all the punctuation marks that *some* other languages require.
    > Remember the Dilbert where PHB complains that his programmers are using
    > way too many semicolons? :)


    Don't get me started on code reviews by non-programmers.

    Somewhere I have one saved, that I still can't figure out.



    --
    UNIX/Perl/C/Pizza____________________s h a n n o n@wido !SPAM maker.com


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    Charles Shannon Hendrix, Jul 8, 2003
    #19
  20. Bob Gailer

    Larry__Weiss Guest

    Re: Collective memory (was: Good code patterns in Python)

    Dennis Lee Bieber wrote:
    > (we had an instructor at my College who punched a C in column 80 as a
    > standard practice -- he could flip the card around to document changes
    > -- it would be treated as a comment by the compiler, and a listing
    > would show the text right-to-left)
    >


    For some forms of job control you could create a card that had two
    choices punched starting from each end of the card. You could simply
    flip the card to invert the selection.

    I even remember once coding and using a text-editor macro to do something
    similar with the current line of text.
    Larry__Weiss, Jul 9, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhijit Mhatre
    Replies:
    2
    Views:
    435
  2. crichmon
    Replies:
    4
    Views:
    477
    Mabden
    Jul 7, 2004
  3. Will Stuyvesant

    Good code patterns in Python

    Will Stuyvesant, Jul 1, 2003, in forum: Python
    Replies:
    10
    Views:
    636
    Ben Finney
    Jul 3, 2003
  4. Michael Chermside

    Good code patterns in Python

    Michael Chermside, Jul 1, 2003, in forum: Python
    Replies:
    2
    Views:
    326
    Max M
    Jul 3, 2003
  5. Michael Sparks
    Replies:
    2
    Views:
    269
    Max M
    Jul 7, 2003
Loading...

Share This Page