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]
Code Like a Pythonista: Idiomatic Python
21–30 of 59 posts
Re: Code Like a Pythonista: Idiomatic Python
#22Re: Code Like a Pythonista: Idiomatic Python
#23Re: Code Like a Pythonista: Idiomatic Python
#24In 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
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
#25I 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
#26One 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…
Re: Code Like a Pythonista: Idiomatic Python
#27One 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…
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
#28One 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…
Re: Code Like a Pythonista: Idiomatic Python
#29One 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…
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.
Re: Code Like a Pythonista: Idiomatic Python
#30Can anyone suggest any other materials or resources in the same vein as this?