84
votes
1answer
2k 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 ...
64
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, ...
34
votes
3answers
33k 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, ...
26
votes
5answers
2k 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__', ...
19
votes
3answers
911 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
432 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
4answers
2k 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 ...
17
votes
1answer
414 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
6answers
573 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 ...
15
votes
4answers
8k 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,", ...
15
votes
3answers
665 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
2answers
824 views

End of support for python 2.7?

Is there a known date/timeframe when python 2.7 will not be supported any more in favor of python 3?
13
votes
3answers
2k views

A Python 2.7, 3.0 Question for Pythonistas - Best Practices?

I am learning to program in Python and we have version 2.7 installed at work. Whenever I try to dive deep into Python, I never liked the dea of diving into a deprecated version (2.7). My work is not ...
13
votes
2answers
939 views

Multiple keys per value

Is it possible to assign multiple keys per value in a Python dictionary. One possible solution is to assign value to each key: dict = {'k1':'v1', 'k2':'v1', 'k3':'v1', 'k4':'v2'} but this is not ...

1 2 3 4 5 273
15 30 50 per page