Tagged Questions
115
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 ...
88
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. ...
77
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, ...
43
votes
3answers
41k 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, ...
29
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__', ...
23
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
992 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) ...
19
votes
2answers
135 views
Why are these two functions different?
Take a look at this:
>>> def f():
... return (2+3)*4
...
>>> dis(f)
2 0 LOAD_CONST 5 (20)
3 RETURN_VALUE
Evidently, the compiler has ...
18
votes
2answers
24k 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 ...
18
votes
7answers
473 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
515 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 ...
1
vote
1answer
45 views
Numpy reshape yields a different size error
I have a piece of Python code which has been used for about a year without any issues (it reads, uncompresses/unpacks data, selects a window and plots it using Numpy/Matplotplib).
We recently got a ...
0
votes
2answers
32 views
Using an Environmental Variables In a Variety of Path Types
How would i go about using the same method for using the USERNAME variable in the top portion of code in the bottom code, i just feel like i don't have a grounded knowledge of the syntax with ...