News for hackers. Stuff that matters.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by Retra on Friday August 07, 2015 @06:04PM (#10025293)
My concern with 80 cols is that it is not friendly to dynamic languages that allow nested definitions, or long variable names.

    if __name__ == '__main__':
        def this_is_a_function(*args, **kwargs):
            def this_is_a_helper_function(arg1, 
                                          arg2=('this line is already indented 39 '
                                                'chars!')):
                this_long_variable = ("Don't put an expression here unless you "
                                      "want to use useless intermediate variabls.")
                return this_long variable if var else 'really short here'
            return 1

And some people want to insist on 8 char indents!
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by sago on Friday August 07, 2015 @06:25PM (#10025386)
Why not follow pep 8's indentation rules:

    def this_is_a_function(*args, **kws):
        def this_is_a_helper_function(
                arg1,
                arg2='this line is indented 12 chars!'):
            this_long_variable = (
                'Don\'t put an expression here unless you '
                'want to use useless intermediate variables')
            return this_long_variable if var else 'really short here'
        return 1
(for the same number of lines) or better still follow some basic common sense:

1. Local functions should be short and have short names since their scope is limited.

2. Local variables, similarly

3. Don't put long explicit strings in code.

and get

    messages = dict(
        long_default = \
            'Don\'t write strings that may wrap in ugly ways on 80 col shells.',
        short_default = 'Keep it short'
        )

    def this_is_a_function(parameter):
        def message(long, message='this is only 36 chars'):
            long_message = messages.long_default
            short_message = message or messages.short_default
            return long_message if long else short_message
        # Do something here.
        return 1
And if you have an expression that is more than 80 characters long, breaking bits of that expression and assigning them to temporary variables is not 'useless', it makes code clearer and is only rarely a performance hit.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by Retra on Friday August 07, 2015 @07:26PM (#10025605)
Those are great suggestions, but may point is more that "too much on one line" isn't necessarily better than "too little over too many lines." It can be hard to get a good overview of what is happening when you've got to resort to splitting one significant token per line.

In other words, I try to use vertical space for higher-level work than horizontal space, and when you have to fit both into vertical space, you lose some clarity in high-level in order to gain it in low-level.

Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by sago on Friday August 07, 2015 @07:36PM (#10025648)
Sorry, I know that putting code up is an invitation for people to pick at the wrong feature, so I realised I was being churlish.

But I think these things do have to be addressed with examples. There are few people willing to put an actual example up that a) is frequent enough to mean that 80cols can't be easily used (note, not 'preferred', we're talking about a standard, plenty of people 'prefer' 80 cols) and b) is pythonic enough that it isn't better written in more explicit steps.

Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by Retra on Friday August 07, 2015 @07:57PM (#10025725)
Eighty columns is a good standard, I just think people forget how small it can be, and why it isn't always a problem. I've written tons of code that looks fantastic, if only I could use 85 columns, but rewriting it to use less just makes use of some pretty clunky constructs. All these extra "()"s and "/"s aren't actually helping make the line more readable, it just makes it fit into a rather arbitrary column limit. And at that point, you'd be thinking that the best way to make your code the most readable it can be is to use more columns.

So I don't think 80 cols is a hard limit, anymore than I think writing code in a terminal is a hard limit. I use a non-terminal text editor. I do it because it makes it easier to read code and format it how I like. I see little reason why anyone should be jumping through hoops to accommodate that. By using a terminal editor, you've already decided you won't get the best formatting, because you sacrifice the flexibility that allows it in exchange for something else. You should be expecting things to wrap around awkwardly on occasion.

Eighty columns is a good target width, but it's not worth proliferating clunky constructs to conform to. That's no better than having code wrap. (Of course, someone could also write terminal editors that allow text to go longer than 80 lines. Why are you using an editor like that if it can't do something this simple?)

Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by sago on Friday August 07, 2015 @08:02PM (#10025741)
Thanks again. I agree, I've written a lot of code that would be clearer in 85 cols than 80, and some code that would have been clearer in 160 cols too.

I think we're agreeing mostly.

The problem with 'ish' is that I see it as worse than useless. 80cols-ish is what? 90 cols max? Why not just use 90 cols as the standard? If I have to scroll my whole window or resize it to read your line, is that really better than if it had an extra break? Some people may think so, but I don't, personally.

Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by Retra on Friday August 07, 2015 @08:14PM (#10025786)
Ok, cool. What I do is just put a column marker at 79, make my window 82 cols wide, and then just write it as beautifully as I can. No hard limits. Just the natural "I can't read that over there, I should maybe rewrite it."
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by coldtea on Friday August 07, 2015 @06:59PM (#10025498)
If I see a "\" I close the editor and delete the source file.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by sago on Friday August 07, 2015 @07:16PM (#10025560)
How impressive of you.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by bpicolo on Friday August 07, 2015 @06:15PM (#10025336)
Then use better whitespace rules. Not to mention the helper isn't capturing any closure here so it's not particularly useful inline, and even when they are you can refactor variable capture to new functions. Nested definitions in python are pretty rare, and oft unnecessary

    if __name__ == '__main__':
        def this_is_a_function(*args, **kwargs):
            def this_is_a_helper_function(
                arg1, 
                arg2='this line is now indented 12 chars!'
            ):
                this_long_variable = (
                    "Don't put an expression here unless you "
                    "want to use useless intermediate variabls."
                )
                return this_long variable if var else 'really short here'
            return 1
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by JadeNB on Saturday August 08, 2015 @04:36PM (#10028594)
Surely the fact that there are two typos in your code, one of them in the name of a variable (`this_long_variable` vs `this_long variable`), indicates that things can go wrong when you abstract out a literal to a variable?
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by bpicolo on Sunday August 09, 2015 @12:45PM (#10030653)
I literally copy pasted from the other guy and added whitespace.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by nvader on Friday August 07, 2015 @07:31PM (#10025629)
To quote the Zen of Python:

    Flat is better than nested.
Too many levels of nesting is definitely a code smell. Often it's better to pull those helper functions out, and/or create function or method factory functions at a higher level of organization.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by Retra on Friday August 07, 2015 @08:07PM (#10025758)
You can't really do that if you're writing closures.
Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by zo1 on Saturday August 08, 2015 @03:07AM (#10026589)
Have you had a look at https://docs.python.org/2/library/functools.html#functools.p... ?

Anywho, do you have a genuine example of where a closure is helpful for you in python? I'm very curious.

Re:Beyond PEP 8 – Best practices for beautiful intelligible ... by Retra on Saturday August 08, 2015 @05:18PM (#10028691)
I don't understand the question. Your link has examples of closures right in it, so naturally if you're doing anything like that, you'll want them.