112
votes
6answers
3k views

What is […] in Python 2.7?

I was playing around in python. I used the following code in IDLE p = [1, 2] p[1:1] = [p] print p The output was [1, [...], 2] What is this […]? Interestingly I could now use this as a list of ...
87
votes
1answer
3k views

Why is early return slower than else?

This is a follow-up question to an answer I gave a few days back. Edit: it seems that the OP of that question already used the code I posted to him to ask the same question, but I was unaware of it. ...
76
votes
2answers
2k views

str performance in python

While profiling a piece of python code (python 2.6 up to 3.2), I discovered that the str method to convert an object (in my case an integer) to a string is almost an order of magnitude slower than ...
65
votes
1answer
2k views

Python: why are * and ** faster than / and sqrt()?

While optimising my code I realised the following: >>> from timeit import Timer as T >>> T(lambda : 1234567890 / 4.0).repeat() [0.22256922721862793, 0.20560789108276367, ...
42
votes
3answers
40k views

How to uninstall Python 2.7 on a Mac OS X 10.6.4?

I want to completely remove Python 2.7 from my Mac OS X 10.6.4. I managed to remove the entry from the PATH variable by reverting my .bash_profile. But I also want to remove all directories, files, ...
28
votes
5answers
3k views

Python: What's the difference between __builtin__ and __builtins__?

I was coding today and noticed something. If I open a new interpreter session (IDLE) and check what's defined with the dir function I get this: $ python >>> dir() ['__builtins__', '__doc__', ...
22
votes
4answers
9k views

Why is parenthesis in print voluntary in Python 2.7?

In Python 2.7 both the following will do the same print("Hello, world!") # Prints "Hello, world!" print "Hello, world!" # Prints "Hello, world!" However the following will not print("Hello,", ...
22
votes
4answers
3k views

What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?

Note: I'm using Python 2.7, and pySerial for serial communications. I found this article which lists two ways: http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports ...
21
votes
3answers
982 views

Python — what is NOT in 2.7 that IS in 3.1? So many things have been back-ported, what is NOT?

I've been following the saga of Python 3.x and have watched the 3.x features gradually getting back-ported to the 2.x line. Most of the libraries I use haven't been ported and some (e.g. Twisted) ...
18
votes
7answers
470 views

Constructing the largest number possible by rearranging a list

Say I have an array of positive whole integers; I'd like to manipulate the order so that the concatenation of the resultant array is the largest number possible. For example [97, 9, 13] results in ...
18
votes
1answer
500 views

Huge memory leak in repeated os.path.isdir calls?

I've been scripting something that has to do with scanning directories and noticed a severe memory leak when calling os.path.isdir, so I've tried the following snippet: def func(): if not ...
16
votes
2answers
22k views

Installing Numpy on 64bit Windows 7 with Python 2.7.3

It looks like the only 64 bit windows installer for Numpy is for Numpy version 1.3.0 which only works with Python 2.6 http://sourceforge.net/projects/numpy/files/NumPy/ It strikes me as strange that ...
16
votes
6answers
608 views

Does this prime function actually work?

Since I'm starting to get the hang of Python, I'm starting to test my newly acquired Python skills on some problems on projecteuler.net. Anyways, at some point, I ended up making a function for ...
16
votes
3answers
1k views

memoization library for python 2.7

I see that python 3.2 has memoization as a decorator in functools library. http://docs.python.org/py3k/library/functools.html#functools.lru_cache Unfortunately it is not yet backported to 2.7. Is ...
14
votes
5answers
4k views

Redirecting stdout to “nothing” in python

I have a large project consisting of sufficiently large number of modules, each printing something to the standard output. Now as the project has grown in size, there are large no. of print statements ...

1 2 3 4 5 374
15 30 50 per page