Python Newbie

Discussion in 'Python' started by Piterrr, Feb 21, 2013.

  1. Piterrr

    Piterrr Guest

    Hi folks.
    I am a long time C sharp dev, just learning Python now due to job requirements. My initial impression is that Python has got to be the most ambiguous and vague language I have seen to date. I have major issues with the fact that white space matters. How do you deal with this? For example, you open asource file in different editors and the indentation levels change even though i only have spaces, no tabs (compare Windows Notepad and Notepad++). Which editor do you trust? In addition, code is difficult to read because you cannot lay it out in easily discernable blocks. For example, I always tend to indent a full 'if' statement block so that it is easier to see where the if block starts and ends. Can't do that in Python. What is even more frustrating is that Python is inconsistent with its syntax. For example, when I write "if (myVariable != 0):" then this is OK but "for (i in intAry):" results in syntax error. Apparently Python has problems with my use of parentheses. How retarded. I think I will rather find another job than eat my nerves with Python.
    Any comments on this before I quit my job?
     
    Piterrr, Feb 21, 2013
    #1
    1. Advertising

  2. Piterrr

    Ian Kelly Guest

    On Thu, Feb 21, 2013 at 2:26 PM, Piterrr <> wrote:
    > I am a long time C sharp dev, just learning Python now due to job requirements. My initial impression is that Python has got to be the most ambiguous and vague language I have seen to date. I have major issues with the factthat white space matters. How do you deal with this? For example, you opena source file in different editors and the indentation levels change even though i only have spaces, no tabs (compare Windows Notepad and Notepad++).Which editor do you trust?


    I have never had this problem (although I don't use either of those
    editors either). Are you sure you don't have any tabs in there?

    > In addition, code is difficult to read because you cannot lay it out in easily discernable blocks. For example, I always tend to indent a full 'if' statement block so that it is easier to see where the if block starts and ends. Can't do that in Python.


    I don't understand what it is that you want to do but can't. Can you
    give an example? The whole point of making indentation matter in
    Python is to *force* the programmer to lay out their blocks in a
    consistent and easily discernible manner.

    > What is even more frustrating is that Python is inconsistent with its syntax. For example, when I write "if (myVariable != 0):" then this is OK but "for (i in intAry):" results in syntax error. Apparently Python has problems with my use of parentheses.


    The former works because the if statement only takes one expression,
    and the parentheses are interpreted as part of that expression, not
    part of the syntax for the if. The for statement requires both a
    variable name and an expression, distinguished by syntax, which again
    includes no parentheses. You could surround just the expression part
    in parentheses if you like -- "for i in (intAry):" -- but it looks
    pretty weird if you ask me.

    I don't know why you should expect to be able to add parentheses to
    arbitrary syntax and have it just work. It strikes me as being like
    trying to declare a variable as "int (x=3);" in C# (which I haven't
    tested, but I doubt that is valid syntax).
     
    Ian Kelly, Feb 21, 2013
    #2
    1. Advertising

  3. Piterrr

    MRAB Guest

    On 2013-02-21 21:26, Piterrr wrote:
    > Hi folks. I am a long time C sharp dev, just learning Python now due
    > to job requirements. My initial impression is that Python has got to
    > be the most ambiguous and vague language I have seen to date. I have
    > major issues with the fact that white space matters. How do you deal
    > with this? For example, you open a source file in different editors
    > and the indentation levels change even though i only have spaces, no
    > tabs (compare Windows Notepad and Notepad++). Which editor do you
    > trust? In addition, code is difficult to read because you cannot lay
    > it out in easily discernable blocks. For example, I always tend to
    > indent a full 'if' statement block so that it is easier to see where
    > the if block starts and ends. Can't do that in Python. What is even
    > more frustrating is that Python is inconsistent with its syntax. For
    > example, when I write "if (myVariable != 0):" then this is OK but
    > "for (i in intAry):" results in syntax error. Apparently Python has
    > problems with my use of parentheses. How retarded. I think I will
    > rather find another job than eat my nerves with Python. Any comments
    > on this before I quit my job?
    >

    Python isn't C#. It has a different syntax.

    Pascal isn't C# either.

    Pascal would accept:

    if (myVariable <> 0) then
    ...

    and reject:

    for (i := 0 to 9) do
    ...

    As for the indentation, it shouldn't change if you're using only spaces
    (not unless you're using a proportional font!).
     
    MRAB, Feb 21, 2013
    #3
  4. On Fri, Feb 22, 2013 at 8:26 AM, Piterrr <> wrote:
    > Hi folks.
    > I am a long time C sharp dev, just learning Python now due to job requirements. My initial impression is that Python has got to be the most ambiguous and vague language I have seen to date. I have major issues with the factthat white space matters. How do you deal with this? For example, you opena source file in different editors and the indentation levels change even though i only have spaces, no tabs (compare Windows Notepad and Notepad++).Which editor do you trust? In addition, code is difficult to read because you cannot lay it out in easily discernable blocks. For example, I always tend to indent a full 'if' statement block so that it is easier to see wherethe if block starts and ends. Can't do that in Python. What is even more frustrating is that Python is inconsistent with its syntax. For example, when I write "if (myVariable != 0):" then this is OK but "for (i in intAry):" results in syntax error. Apparently Python has problems with my use of parentheses. How retarded. I
    > think I will rather find another job than eat my nerves with Python.
    > Any comments on this before I quit my job?


    The first comment I would make is this: Every language you learn MUST
    teach you something new about programming, otherwise you haven't
    really learned a new language (just a new dialect of an old one).
    Embrace Python's differences, get to know how things work, then make
    your decision as to what you like and what you don't :)

    When there's a question of trust involving Windows Notepad, by default
    trust the other option. Notepad *sucks* for pretty much everything.
    Notepad++ is far more reliable. So is NoteTab, so is SciTE, so is
    pretty much anything else on the market (though the Open Watcom editor
    is designed exclusively for C, and may not be suitable for Python - it
    has a habit of converting tabs to spaces, so you may run into
    problems).

    The issue of parentheses is one of syntax. Anything that's an
    expression can have an extra set of parens around it; anything that's
    not, can't. The same will happen in most languages; I daresay C# won't
    let you put parentheses around the semicolon at the end of a
    statement, for instance. In a for loop, the 'in' keyword separates two
    expressions, so you can't have parens around it.

    I'm not sure what you mean by "indenting a full 'if' statement block".
    Do you mean this:

    code
    code
    if (some condition) {
    code
    code
    }
    code
    code

    ? Because that's a distinctly weird way to lay out code, and would be
    against the style guides of many organizations (definitely against any
    style guide that I write). Python forces you to use one of the more
    common styles:

    code
    code
    if (some condition) {
    code
    code
    }
    code
    code

    Since the braces are omitted in Python, the same Python code can
    equally well represent the C standards of OTBS (as shown above),
    Allman (with the opening brace on the next line, which is my
    preference when writing C)... actually, every style in
    http://en.wikipedia.org/wiki/Indent_style#Styles follows the pattern
    of having 'if' not indented and the body indented, which is what
    Python enforces. So this might be a change for you, but if you quit
    your job, chances are you'll find an identical change as you conform
    to some other workplace's style guide :)

    Of course, Python does allow and encourage the use of blank lines to
    help lay out your code. So if you're having trouble with the
    readability of statements, try judiciously adding blanks before and/or
    after, and see if that helps.

    And if all that doesn't make you happy with Python, then do look for a
    better job. Not every language is for everyone, and you'll produce
    better code as a contented C# programmer than as a miffed Python
    programmer :) But do consider learning multiple languages. Eric
    Raymond states in one of his essays [1] that you would do well to
    learn five basic languages: Python, C/C++, Java, Perl, and LISP.
    They're distinctly different from each other, and will teach different
    things about how to *code*, which is a skill separate from how to
    *code C* or how to *code Perl*.

    [1] http://www.catb.org/esr/faqs/hacker-howto.html

    ChrisA
     
    Chris Angelico, Feb 21, 2013
    #4
  5. On Thu, 21 Feb 2013 13:26:41 -0800 (PST), Piterrr wrote:

    > I am a long time C sharp dev, just learning Python now due
    > to job requirements. My initial impression is that Python
    > has got to be the most ambiguous and vague language I have
    > seen to date. I have major issues with the fact that white
    > space matters. How do you deal with this?

    [snip]
    > Any comments on this before I quit my job?


    Nope. Quit immediately.

    --
    To email me, substitute nowhere->spamcop, invalid->net.
     
    Peter Pearson, Feb 21, 2013
    #5
  6. Piterrr

    Dave Angel Guest

    On 02/21/2013 04:26 PM, Piterrr wrote:
    > Hi folks.
    > I am a long time C sharp dev, just learning Python now due to job requirements. My initial impression is that Python has got to be the most ambiguous and vague language I have seen to date. I have major issues with the fact that white space matters. How do you deal with this? For example, you open a source file in different editors and the indentation levels change even though i only have spaces, no tabs (compare Windows Notepad and Notepad++). Which editor do you trust?



    I'll take a chance and assume you're not just trolling.

    Spend a while with it, you'll learn to like it. It's about the 35th
    language I've used on the job, and is certainly my favorite. And the
    fact that indentation has to match the meaning is a key advantage over
    languages that encourage you to write code that reads differently to the
    human than to the compiler. Many times I've spotted code written by
    others that either had an extra semicolon, or had a dangling else that
    was lined up with a different if than the compiler would use. There are
    plenty of defensive techniques in the C-family, like requiring braces
    for every clause even if a single statement. But I find the lack of
    braces to make it easier to see a whole function in one view.

    I did spend some time working in C#, but it didn't have a name yet.
    They called it C++ IJW, and I had to practically sign in blood to get a
    copy. A number of the introspection features of dot-net were a result
    of my requests. I had software in Microsoft's booth at the announcement
    of dot-net. I haven't touched dot-net since.

    I decided over 30 years ago (in a spec I wrote for one of my developers)
    that tabs in source code were a huge mistake, because of the varied way
    that different editors, printers, etc. handled them. Consequently, I
    only use editors that have a way to always expand tabs to spaces. I
    consider the tab key just a way to position myself on the screen, and
    would use a different method if I ever wanted a 09h code point in the file.

    Contrary to your experience, I've never seen different text editors
    interpret the columns differently in the absence of tabs. Are you by
    any chance using a proportional font???? Text files must be used with
    fixed-width fonts. In any case, avoid Notepad. It can't even handle
    text files with Unix line-endings. I use emacs, but I also use Komodo,
    gedit, and in the past have used Kedit, Codewright, and many others.


    > In addition, code is difficult to read because you cannot lay it out
    > in easily discernable blocks. For example, I always tend to indent a
    > full 'if' statement block so that it is easier to see where the
    > if block starts and ends. Can't do that in Python.


    The line immediately preceding an indented section is the dependent
    clause, be it if, or else, or def, or class, or ... The section is
    complete when indentation returns to the earlier level. Simple,
    consistent, easy to spot. Unless you use two-column indentation, or
    have 6 levels of indentation in a single function.

    > What is even more frustrating is that Python is inconsistent with
    > its syntax. For example, when I write "if (myVariable != 0):"
    > then this is OK but "for (i in intAry):" results in syntax error.
    > Apparently Python has problems with my use of parentheses.
    > How retarded.


    The if statement takes a single expression, so you can use redundant
    parentheses to your heart's content. The for statement is defined as:

    for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

    where in is a keyword, not part of some expression. So of course the
    parenthesis you tried is illegal.

    To see the rest of the grammar, see
    http://docs.python.org/2/reference/grammar.html
    or http://docs.python.org/3.3/reference/grammar.html

    > I think I will rather find another job than eat my nerves with Python.
    > Any comments on this before I quit my job?
    >


    Finding a new job can be a good thing, if your old company forces you to
    use the same language, and to do the same work over and over again. My
    career has been full of variety, and I've turned down jobs that were
    offered merely because I already knew the tools.

    However, if you give Python a chance, I think it'll grow on you.

    --
    DaveA
     
    Dave Angel, Feb 21, 2013
    #6
  7. Piterrr

    Guest

    Thanks to all for quick relies.

    Chris, you are (almost) spot on with the if blocks indentation. This is what I do, and it has served me well for 15 years.

    code
    code

    if (some condition)
    {
    code
    code
    }

    code
    code

    This is what I call code clarity. With Python, I am having to do this

    code
    code

    ##############################

    if (some condition):
    code
    code

    ##############################

    code
    code

    It does the job, but is not ideal.

    I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Pythonwithout 'i' being declared earlier. How do I know that I haven't used thisvariable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.

    You see, Javascript, for one, behaves the same way as Python (no variable declaration) but JS has curly braces and you know the variable you have justused is limited in scope to the code within the { }. With Python, you haveto search the whole file.

    Thanks to Chris, Ian and Dave for explaining the () issue around if and forstatement. I don't agree with this, but I understand your points. The reason why I like parentheses is because they help with code clarity. I am obsessed with this. :) After all, there is a reason why so many languages haverequired them for several decades.


    What about Python's ambiguity?
    For example, in C you would write

    if (myVar != 0)
    do something

    in Python, this is legal

    if (not myVar):
    do something

    What does this mean? Is it a test for myVar being equal to zero or a test for null, or else?

    I want to learn a new language but Python's quirks are a bit of a shock to me at this point. I have been Pythoning only for about a week.

    In the mean time, thanks to most of you for encouraging me to give Python achance. I will do my best to like it, w/o prejudice.

    Peter
     
    , Feb 21, 2013
    #7
  8. On Fri, Feb 22, 2013 at 9:40 AM, <> wrote:
    > Thanks to all for quick relies.
    >
    > Chris, you are (almost) spot on with the if blocks indentation. This is what I do, and it has served me well for 15 years.
    >
    > code
    > code
    >
    > if (some condition)
    > {
    > code
    > code
    > }
    >


    I understand you have the freedom to do that in most languages, but
    that's simply a restriction of Python: you have to use a more
    conventional indentation system. Personally, I don't see the 'if' as
    being special here, and the body of it (which IS special, as it may or
    may not be executed) is already standing out.

    > I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Python without 'i' being declared earlier. How do I know that I haven't used this variable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.
    >
    > You see, Javascript, for one, behaves the same way as Python (no variabledeclaration) but JS has curly braces and you know the variable you have just used is limited in scope to the code within the { }. With Python, you have to search the whole file.


    That's a fair point. The elision of variable declarations is a design
    decision that Python is unlikely to reverse, and it happens to be one
    I disagree with. But it's there, so when I code Python, I accept it :)
    There are other languages similar to Python that have strict variable
    declarations, and with them the awesomeness that is infinitely nested
    scopes; Python just has global (module) scope and function scope, plus
    'nonlocal' which can solve some problems (but I've yet to find it do
    so without damaging code clarity).

    > What about Python's ambiguity?
    > For example, in C you would write
    >
    > if (myVar != 0)
    > do something
    >
    > in Python, this is legal
    >
    > if (not myVar):
    > do something
    >
    > What does this mean? Is it a test for myVar being equal to zero or a testfor null, or else?


    Python has a state called "truthiness". Generally speaking, something
    with content is true, and something without content is false. The None
    singleton, empty lists, empty tuples, the integer 0, and a regular
    expression result that didn't match, all evaluate as False in a
    boolean context; most other things evaluate as True. If you
    specifically care if it's not zero, you ask if it's not zero; if you
    want to know if it's falsy, you check "not X". The facilities are
    there to do whatever you want.

    ChrisA
     
    Chris Angelico, Feb 21, 2013
    #8
  9. On 21 February 2013 22:40, <> wrote:
    > Thanks to all for quick relies.
    >
    > Chris, you are (almost) spot on with the if blocks indentation. This is what I do, and it has served me well for 15 years.
    >
    > code
    > code
    >
    > if (some condition)
    > {
    > code
    > code
    > }
    >
    > code
    > code


    So you already indent blocks in an "if" construct? This is good
    practise in some languages and is enforced in Python. Once I got used
    to it I found that the compulsory whitespace made it easier to read
    conditional code blocks.

    >
    > This is what I call code clarity. With Python, I am having to do this
    >
    > code
    > code
    >
    > ##############################
    >
    > if (some condition):
    > code
    > code
    >
    > ##############################
    >
    > code
    > code
    >
    > It does the job, but is not ideal.


    Do you mean that you literally insert a line of '#' characters before
    and after in "if" block? There's no need to do that. Just allow
    yourself to acclimatise to the significant whitespace and you'll find
    that it's easy to see where the block begins and ends.

    >
    > I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Python without 'i' being declared earlier. How do I know that I haven't used this variable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.
    >
    > You see, Javascript, for one, behaves the same way as Python (no variabledeclaration) but JS has curly braces and you know the variable you have just used is limited in scope to the code within the { }. With Python, you have to search the whole file.


    No, you only have to search the whole function which for me is rarely
    more than 20 lines. The statement "i=0" when inside a function will
    not overwrite anything outside the function (unless you use the
    global/nonlocal statements). I rarely use global variables or module
    level variables and if I do then I usually have a special place in a
    module/script for defining them. I also tend to name them in ALLCAPS
    just like C-preprocessor macros that need to be carefully maintained
    in a separate "namespace".

    >
    > Thanks to Chris, Ian and Dave for explaining the () issue around if and for statement. I don't agree with this, but I understand your points. The reason why I like parentheses is because they help with code clarity. I am obsessed with this. :) After all, there is a reason why so many languages have required them for several decades.


    You'll get used to using the colon in the same way.

    >
    > What about Python's ambiguity?
    > For example, in C you would write
    >
    > if (myVar != 0)
    > do something
    >
    > in Python, this is legal
    >
    > if (not myVar):
    > do something
    >
    > What does this mean? Is it a test for myVar being equal to zero or a testfor null, or else?


    All of those things. It executes "do something" if myVar is
    1) zero (whether int/float/complex etc.)
    2) False
    3) None
    4) an empty collection (list/set/tuple etc.)
    5) an empty string
    6) and more...

    If the context doesn't make it clear what you are testing for then use
    a more specific test (myVar!=0 works just as well).

    > I want to learn a new language but Python's quirks are a bit of a shock to me at this point. I have been Pythoning only for about a week.
    >
    > In the mean time, thanks to most of you for encouraging me to give Pythona chance. I will do my best to like it, w/o prejudice.


    Many of the things that have confused/concerned you are things that I
    actually like about Python. Given time you may do as well.


    Oscar
     
    Oscar Benjamin, Feb 21, 2013
    #9
  10. Piterrr

    Guest

    Hi Chris,

    Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in addition to writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - haveto study the code in detail to see what it is testing.

    I could show more examples of what I find confusing in existing code, but Idon't intent to troll. I'm just trying to understand the language as it is.. I will see how it goes.

    Pete
     
    , Feb 21, 2013
    #10
  11. Piterrr

    Guest

    Hi Chris,

    Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in addition to writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - haveto study the code in detail to see what it is testing.

    I could show more examples of what I find confusing in existing code, but Idon't intent to troll. I'm just trying to understand the language as it is.. I will see how it goes.

    Pete
     
    , Feb 21, 2013
    #11
  12. On 21/02/2013 23:34, wrote:
    > Hi Chris,
    >
    > Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in addition to writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - have to study the code in detail to see what it is testing.
    >
    > I could show more examples of what I find confusing in existing code, but I don't intent to troll. I'm just trying to understand the language as it is. I will see how it goes.
    >
    > Pete
    >


    Perhaps look at code in the standard library to get a feel for things?
    This should help you overcome OCD, i.e. Obsessive C Disorder :)

    --
    Cheers.

    Mark Lawrence
     
    Mark Lawrence, Feb 21, 2013
    #12
  13. Piterrr

    Ian Kelly Guest

    On Thu, Feb 21, 2013 at 3:40 PM, <> wrote:
    > I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Python without 'i' being declared earlier. How do I know that I haven't used this variable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.


    If you need to search for variable names to see if you're overwriting
    something, then your functions are too large and should probably be
    refactored, or you're abusing globals, or possibly you just haven't
    fully understood Python's scoping rules.

    > You see, Javascript, for one, behaves the same way as Python (no variabledeclaration) but JS has curly braces and you know the variable you have just used is limited in scope to the code within the { }. With Python, you have to search the whole file.


    Er, this doesn't sound right at all. Javascript does have variable
    declarations, using the "var" keyword. Within a function, a declared
    variable has local scope, but an undeclared variable has global scope.
    Despite having curly braces, Javascript does not have block scopes
    like C# does. More details can be found here:

    http://stackoverflow.com/questions/500431/javascript-variable-scope

    In Python, on the other hand, an undeclared variable in a function is
    local by default (unless it is never assigned to). To me, this makes
    Python win out over Javascript because you're never going to
    accidentally create a global variable just by failing to declare the
    variable in the function where you use it.
     
    Ian Kelly, Feb 21, 2013
    #13
  14. On Fri, Feb 22, 2013 at 10:34 AM, <> wrote:
    > Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in additionto writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - have to study the code in detail to see what it is testing.


    It's testing something - specifically, whether or not the object wants
    to "feel true" or "feel false". It's up to the object to define what
    that means.

    Most of the time, it's exactly what you want. When it isn't, you can
    put an exact comparison.

    ChrisA
     
    Chris Angelico, Feb 22, 2013
    #14
  15. On Thu, 21 Feb 2013 13:26:41 -0800 (PST), Piterrr
    <> declaimed the following in
    gmane.comp.python.general:

    > For example, you open a source file in different editors and the indentation levels change even though i only have spaces, no tabs (compare Windows Notepad and Notepad++).


    Is one using a proportional spaced font while the other is using a
    mono spaced font?

    > yntax. For example, when I write "if (myVariable != 0):" then this is OK but "for (i in intAry):" results in syntax error. Apparently Python has problems with my use of parentheses. How retarded. I


    No -- (i in intAry) is returning a single boolean result (is the
    value of "i" IN the value of "intAry"), so "for (i in intAry)" becomes
    the equivalent of "for True" or "for False" depending on the result of
    the "in" operator.
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Feb 22, 2013
    #15
  16. On 02/21/2013 04:26 PM, Piterrr wrote:
    > Hi folks.
    > I am a long time C sharp dev, just learning Python now due to job
    > requirements. My initial impression is that Python has got to be the
    > most ambiguous and vague language I have seen to date. I have major
    > issues with the fact that white space matters. How do you deal with
    > this? For example, you open a source file in different editors and the
    > indentation levels change even though i only have spaces, no tabs
    > (compare Windows Notepad and Notepad++). Which editor do you trust? In
    > addition, code is difficult to read because you cannot lay it out in
    > easily discernable blocks. For example, I always tend to indent a full
    > 'if' statement block so that it is easier to see where the if block
    > starts and ends. Can't do that in Python. What is even more
    > frustrating is that Python is inconsistent with its syntax. For
    > example, when I write "if (myVariable != 0):" then this is OK but "for
    > (i in intAry):" results in syntax error. Apparently Python has
    > problems with my use of parentheses. How reta
    > rded. I
    > think I will rather find another job than eat my nerves with Python.
    > Any comments on this before I quit my job?



    I think you need to distinguish between your own stylistic preference
    and considerations of writing in a style that is clear and readable for
    other developers who will be working with your code.

    For example:

    ....

    if condition {
    ...
    }

    ....


    Looks very unclear and confusing to me. Whether it's C# or ruby or
    anything else, most devs don't indent like that; and that raises a
    question - was an outer block accidentally deleted? How do I know if it's
    your personal style or a mistake?

    Even if I know it is intentional, it's not clear why an if block is
    treated in a special way... what about for loops, try/except blocks,
    etc? To me, if operator being at the same indentation indicates that it
    is executed after the statements that precede it and before the ones
    that follow it -- can't get any clearer than that!

    Things like:

    if (condition == value)

    look like unnecessary visual noise to me. Parens are useful and are
    meant to group conditions when their number becomes unwieldy and hard to
    read and to override operator order of precendence (or rather to avoid
    having to remember it):

    if (condition == value * modifier) or (condition2 == default_value):

    In regard to "truthy" evaluation -- it makes more sense if variables
    have good names, as they should; for example:

    if mylist: .. do something with the list ..

    if mystring: .. process string ..

    if my_things_count:
    .. there were some things, process them ..
    else:
    .. there were no things! ..


    In all of these cases, my intent is to ask if the value is empty or not,
    or blank, or holds some non-false contents. It may also be a custom
    object which is responsible for telling me, in a standardized way, if
    it's blank/False or not. In 99.8% of cases, do I care if mystring is
    None or '' or False? Of course not! Do I care if mylist is None or []?
    Almost certainly not.

    And the nice thing is, in the rare case that you do care, you'll
    write 'if mylist is None:' -- and that alerts the reader to the fact
    that mylist's specific value is important.

    Reducing unnecessary visual noise in the code is a worthy goal -- it
    lets your eyes focus more on the logic of the program.

    HTH, -m


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    Language is like money, without which specific relative values may well
    exist and be felt, but cannot be reduced to a common denominator.
    George Santayana
     
    Mitya Sirenef, Feb 22, 2013
    #16
  17. Piterrr

    rusi Guest

    On Feb 22, 3:40 am, wrote:
    > Thanks to all for quick relies.
    >
    > Chris, you are (almost) spot on with the if blocks indentation. This is what I do, and it has served me well for 15 years.
    >
    > code
    > code
    >
    >    if (some condition)
    >    {
    >       code
    >       code
    >    }
    >
    > code
    > code
    >
    > This is what I call code clarity. With Python, I am having to do this
    >
    > code
    > code
    >
    > ##############################
    >
    > if (some condition):
    >   code
    >   code
    >
    > ##############################
    >
    > code
    > code
    >
    > It does the job, but is not ideal.
    >
    > I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Python without 'i' being declared earlier. How do I know that I haven't used this variable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.
    >
    > You see, Javascript, for one, behaves the same way as Python (no variabledeclaration) but JS has curly braces and you know the variable you have just used is limited in scope to the code within the { }. With Python, you have to search the whole file.
    >
    > Thanks to Chris, Ian and Dave for explaining the () issue around if and for statement. I don't agree with this, but I understand your points. The reason why I like parentheses is because they help with code clarity. I am obsessed with this. :) After all, there is a reason why so many languages have required them for several decades.
    >
    > What about Python's ambiguity?
    > For example, in C you would write
    >
    > if (myVar != 0)
    >   do something
    >
    > in Python, this is legal
    >
    > if (not myVar):
    >   do something


    I have seen any amount of C code where:
    if (x != 0) { ...

    is shortened to

    if (x) { ...

    Python is similar (and many of us dont like this feature).
    Just know it as a feature and get on with it. Python has less
    features/gotchas/bugs than most other languages.

    As for indentation: This is more a philosophy than a technology
    question. Its called DRY (Dont Repeat Yourself) In most languages
    (other than python and haskell) you use {} or some such for the
    compiler and indentation for the human reader. Saying something
    exactly once is not just a space/time saver. Its also a error saver.

    > In the mean time, thanks to most of you for encouraging me to give Pythona chance. I will do
    > my best to like it, w/o prejudice.


    The mathematician Paul Halmos was once told by a bewildered student:
    "I just dont understand mathematics!"
    Halmos replied: My boy you dont understand mathematics. You just get
    used to it.
     
    rusi, Feb 22, 2013
    #17
  18. On Thu, 21 Feb 2013 14:40:37 -0800, piterrr.dolinski wrote:

    > With Python, I am having to do this
    >
    > code
    > code
    >
    > ##############################
    >
    > if (some condition):
    > code
    > code
    >
    > ##############################



    I prefer this:


    # WARNING WARNING WARNING IF STATEMENT AHEAD BEWARE BEWARE BEWARE
    if something():
    do(stuff)
    # WHEW IF STATEMENT FINISHED!!! WE SURVIVED, THANK THE GODS!!!


    That way I can be sure that people reading it will know it is an "if" and
    not a "for" or "while", just in case the actual code wasn't clear enough.



    > I am nervous about using variables "out of the blue", without having to
    > declare them.


    I feel your pain. I'm nervous about declaring variables "out of the
    blue", without having to declare that I'm about to declare them. I live
    in hope that some day I will find a language that lets me write:

    FORWARD VARS:
    int x;
    double y;
    str s;

    VARS:
    int x;
    float y;
    str s;


    and have the compiler catch my error in declaring y as a float instead of
    a double. Then I will be one step closer to my dream of not having to
    understand the code I write.


    > For example, when I write "i = 0" it is perfectly OK to
    > Python without 'i' being declared earlier. How do I know that I haven't
    > used this variable earlier and I am unintentionally overwriting the
    > value? I find I constantly have to use the search facility in the
    > editor, which is not fun.


    Some people suggest that functions should be small enough to read over in
    a glance, or at least a couple of glances. They even say that variable
    names should be meaningful, so you can tell whether a variable has been
    used from context. I say, fi to that! Programming should be a challenge!
    It should be exciting! Which is why I never use functions, and always use
    the same single-letter variable with a cryptographic hash to distinguish
    them.

    i048b8497cf86dab9dade2ce6beddf13a = []
    i048b6497cd85d9b9da2a3ce6bdedf167 = 42


    *wink*


    I'm not going to apologise for taking the piss, although I do hope you
    will take it in the spirit it is intended: constructive criticism rather
    than abuse. The reality is, these "problems" you are running into are not
    problems in practice, and the "solutions" you are used to, like braces,
    are visually noisy and not terribly effective. You would probably think
    differently of { } if they were spelled "BEGIN" and "END" like in Pascal.

    Braces have one, and only one, advantage: if you pass code through a
    noisy environment that deletes whitespace at will, you can recover the
    structure of the code from the braces. Python does not have that
    advantage, and sometimes it is a pain, e.g. when people email code using
    a broken mail client that deletes spaces.

    But then, if you passed C code through something that deleted the braces,
    you'd be royally screwed too. The solution to that is to stop having
    tools that think that whitespace isn't significant. Of course it is
    significant, it is significant to the human reader, who is about a
    hundred billion trillion times more important than the compiler.



    --
    Steven
     
    Steven D'Aprano, Feb 22, 2013
    #18
  19. Dear Mr D'Aprano,

    I thank you for your post but I must complain in the strongest possible
    terms that it was not enclosed in the correct delimeters. It should
    have been enclosed in a <humour>... </humour> pair (or
    <humor>...</humor> if you are American).

    I was drinking coffee at the time I started reading your post and,
    entirely due to your negligent lack of proper delimiters, much of my
    coffee is now irretrievably resident in my keyboard. My lawyers will be
    in touch...


    On 22/02/2013 11:26, Steven D'Aprano wrote:
    > On Thu, 21 Feb 2013 14:40:37 -0800, piterrr.dolinski wrote:
    >
    >> With Python, I am having to do this
    >>
    >> code
    >> code
    >>
    >> ##############################
    >>
    >> if (some condition):
    >> code
    >> code
    >>
    >> ##############################

    >
    > I prefer this:
    >
    >
    > # WARNING WARNING WARNING IF STATEMENT AHEAD BEWARE BEWARE BEWARE
    > if something():
    > do(stuff)
    > # WHEW IF STATEMENT FINISHED!!! WE SURVIVED, THANK THE GODS!!!
    >
    >
    > That way I can be sure that people reading it will know it is an "if" and
    > not a "for" or "while", just in case the actual code wasn't clear enough.
    >
    >
    >
    >> I am nervous about using variables "out of the blue", without having to
    >> declare them.

    > I feel your pain. I'm nervous about declaring variables "out of the
    > blue", without having to declare that I'm about to declare them. I live
    > in hope that some day I will find a language that lets me write:
    >
    > FORWARD VARS:
    > int x;
    > double y;
    > str s;
    >
    > VARS:
    > int x;
    > float y;
    > str s;
    >
    >
    > and have the compiler catch my error in declaring y as a float instead of
    > a double. Then I will be one step closer to my dream of not having to
    > understand the code I write.
    >
    >
    >> For example, when I write "i = 0" it is perfectly OK to
    >> Python without 'i' being declared earlier. How do I know that I haven't
    >> used this variable earlier and I am unintentionally overwriting the
    >> value? I find I constantly have to use the search facility in the
    >> editor, which is not fun.

    > Some people suggest that functions should be small enough to read over in
    > a glance, or at least a couple of glances. They even say that variable
    > names should be meaningful, so you can tell whether a variable has been
    > used from context. I say, fi to that! Programming should be a challenge!
    > It should be exciting! Which is why I never use functions, and always use
    > the same single-letter variable with a cryptographic hash to distinguish
    > them.
    >
    > i048b8497cf86dab9dade2ce6beddf13a = []
    > i048b6497cd85d9b9da2a3ce6bdedf167 = 42
    >
    >
    > *wink*
    >
    >
    > I'm not going to apologise for taking the piss, although I do hope you
    > will take it in the spirit it is intended: constructive criticism rather
    > than abuse. The reality is, these "problems" you are running into are not
    > problems in practice, and the "solutions" you are used to, like braces,
    > are visually noisy and not terribly effective. You would probably think
    > differently of { } if they were spelled "BEGIN" and "END" like in Pascal.
    >
    > Braces have one, and only one, advantage: if you pass code through a
    > noisy environment that deletes whitespace at will, you can recover the
    > structure of the code from the braces. Python does not have that
    > advantage, and sometimes it is a pain, e.g. when people email code using
    > a broken mail client that deletes spaces.
    >
    > But then, if you passed C code through something that deleted the braces,
    > you'd be royally screwed too. The solution to that is to stop having
    > tools that think that whitespace isn't significant. Of course it is
    > significant, it is significant to the human reader, who is about a
    > hundred billion trillion times more important than the compiler.
    >
    >
    >
     
    Steve Simmons, Feb 22, 2013
    #19
  20. On Fri, Feb 22, 2013 at 10:05 PM, Steve Simmons <> wrote:
    > I thank you for your post but I must complain in the strongest possible
    > terms that it was not enclosed in the correct delimeters. It should have
    > been enclosed in a <humour>... </humour> pair (or <humor>...</humor> if you
    > are American).


    Steve, you clearly do not understand the vital importance of these
    tags, which is for the reader, not the writer! Steven should have
    enclosed his text in *both* markers, in order that readers familiar
    with only one of the two systems would nevertheless be spared the
    indignity of having to learn the other.

    <signature>
    <name>
    <.sig>
    ChrisA
    </signature>
    </.sig>
    </name>

    .... there, have fun unnesting that...
     
    Chris Angelico, Feb 22, 2013
    #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. Kruno Saho
    Replies:
    0
    Views:
    149
    Kruno Saho
    Apr 7, 2013
  2. Dave Angel
    Replies:
    0
    Views:
    130
    Dave Angel
    Apr 7, 2013
  3. rusi
    Replies:
    0
    Views:
    117
  4. Miki Tebeka
    Replies:
    0
    Views:
    88
    Miki Tebeka
    Apr 7, 2013
  5. Dennis Lee Bieber

    Re: Newbie to python. Very newbie question

    Dennis Lee Bieber, Apr 7, 2013, in forum: Python
    Replies:
    1
    Views:
    101
    Miki Tebeka
    Apr 8, 2013
Loading...

Share This Page