Live data from Hacker News

Code Like a Pythonista: Idiomatic Python

python.net

21–30 of 59 posts

Re: Code Like a Pythonista: Idiomatic Python

#21
post #18
post #15

Earlier quoted context omitted.

This is much better than my own comment - I never even knew about that syntax! Is it efficient though?

My guess is that it's optimized, but I don't know. Semantics-wise, it's consistent with list addition. >>> [0] + [0] + [0] [0, 0, 0] >>> [0] * 3 [0, 0, 0]

I just read, that syntax is sugar for the list.extend() method so it's still making copies of the list - I wouldn't use it for a list containing complex objects; but that is very idiomatic for simple use cases (like this one?).

Re: Code Like a Pythonista: Idiomatic Python

#24
post #10
post #9

In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.

x = [0] * 100

Just make sure that when you do this, you're not initializing it with mutable objects, e.g. lists.

    x = [[0]] * 2
    print x[0], x[1] # prints [0] [0]
    x[0].append(1)
    print x[0], x[1] # prints [0, 1] [0, 1]
That's because the objects inside the repeated list are simply references to the same object internally.

Re: Code Like a Pythonista: Idiomatic Python

#25
One thing that has always bugged me about PEP8 is the advice about trying to keep line length to I basically just completely ignore this part of the guidance, even though I know that in theory it makes it hard to edit code via a terminal. But this is not something we ever do in practice where I work - code on servers gets there only by being checked out of SVN.

I personally find it incredibly irritating when I see people who've split a function call over a couple of lines just to try and meet this criterion, e.g.:

  def __init__(self, first, second, third,
             fourth, fifth, sixth):
Note that if you're creating functions with very long signatures you're probably doing something wrong.

About the only time I make any kind of concession to the line length guidance is when I initialise a big dictionary, e.g.:

  my_dict = {
      ham: eggs,
      bar: quux,
      # Snip 10 lines of code
      rhubarb: custard
  }
(There are often better ways to do this too, but sometimes it's necessary for clarity to build a dictionary all at once like this.)

I'm perfectly happy to initialise dictionaries with a small number of keys just like this:

  my_dict = { ham: eggs, bar: quux, rhubarb: custard }

Re: Code Like a Pythonista: Idiomatic Python

#26

One thing that has always bugged me about PEP8 is the advice about trying to keep line length to I basically just completely ignore this part of the guidance, even though I know that in theory it makes it hard to edit code via a terminal. But this is not something we ever do in practice where I work - code on servers gets there only by being checked out of SVN. I personally find it incredibly irritating when I see pe…

Some people like to split windows vertically, shorter line help them, but splitting lines break greppability, and I am always wondering how to indent them properly.

Re: Code Like a Pythonista: Idiomatic Python

#27

One thing that has always bugged me about PEP8 is the advice about trying to keep line length to I basically just completely ignore this part of the guidance, even though I know that in theory it makes it hard to edit code via a terminal. But this is not something we ever do in practice where I work - code on servers gets there only by being checked out of SVN. I personally find it incredibly irritating when I see pe…

The original reason may be dated, but there are other places where limiting line length is still helpful.

For example, I'll often have my editor open and docs open in a web browser right next to it.

It also helps when viewing side by side diffs.

Re: Code Like a Pythonista: Idiomatic Python

#28

One thing that has always bugged me about PEP8 is the advice about trying to keep line length to I basically just completely ignore this part of the guidance, even though I know that in theory it makes it hard to edit code via a terminal. But this is not something we ever do in practice where I work - code on servers gets there only by being checked out of SVN. I personally find it incredibly irritating when I see pe…

i love the 80char limit. it makes things nice in a terminal, it lets me tile lots of windows across my screen, and it prevents excessively clever but entirely unreadable one-liners.

Re: Code Like a Pythonista: Idiomatic Python

#29

One thing that has always bugged me about PEP8 is the advice about trying to keep line length to I basically just completely ignore this part of the guidance, even though I know that in theory it makes it hard to edit code via a terminal. But this is not something we ever do in practice where I work - code on servers gets there only by being checked out of SVN. I personally find it incredibly irritating when I see pe…

It's easier to read something quickly if your eyes don't have to travel far. This is why newspapers (remember them?) are written in multiple columns across the page.

This may be less of an issue with code, due to its structure, but I still find it much easier to read 80 column code than code that stretches across the screen.

If I'm reading code in an 80 column terminal I prefer the line breaks to come from the human that wrote it, to show and reveal structure, rather than having it arbitrarily broken at 80 columns by the editor.

Another reason is that most of your code won't stretch across the screen, but you need your window sized for the small amount of code that does, losing a bunch of screen estate to empty space.

But I don't worry too much about it.

Post reply on HN