Tagged Questions
132
votes
17answers
48k views
Should I learn Python 2 before 3, or start directly from Python 3? [closed]
I would like to learn python and currently have access to some good python 2 books. However python 3 is not guaranteed to be backward compatible with python 2. If I were to learn python 2 I have to ...
111
votes
4answers
80k views
Convert byte array to Python string
I'm using this code to get standard output from an external program:
>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
The ...
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 ...
76
votes
2answers
2k views
What does -> mean in Python function definitions?
I've recently noticed something interesting when looking at Python 3.3 grammar specification:
funcdef: 'def' NAME parameters ['->' test] ':' suite
The optional 'arrow' block was absent in Python ...
68
votes
3answers
60k views
Python 3 online interpreter / shell [closed]
Does anyone know about an online interpreter like http://codepad.org/ or http://try-python.mired.org/ with Python 3?
53
votes
1answer
1k views
Which classes cannot be subclassed?
Is there any rule about which built-in and standard library classes are not subclassable ("final")?
As of Python 3.3, here are a few examples:
bool
function
operator.itemgetter
slice
I found a ...
51
votes
7answers
25k views
48
votes
8answers
9k views
How to make an immutable object in Python?
Although I have never needed this, it just struck me that making an immutable object in Python could be slightly tricky. You can't just override __setattr__, because then you can't even set attributes ...
45
votes
7answers
8k views
__getattr__ on a module
How can implement the equivalent of a __getattr__ on a class, on a module?
Example
When calling a function that does not exist in a module's statically defined attributes, I wish to create an ...
41
votes
5answers
29k views
Syntax error on print with Python 3
I'm new to python and am currently lost as to why print is giving a syntax here. Hoping someone might be able to point me in the right direction. Thanks
Python 3.0.1 (r301:69561, Feb 13 2009, ...
41
votes
7answers
8k views
How are you planning on handling the migration to Python 3?
I'm sure this is a subject that's on most python developers' minds considering that Python 3 is coming out soon. Some questions to get us going in the right direction:
Will you have a python 2 and ...
38
votes
2answers
1k views
Why are slices in Python 3 still copies and not views?
As I only now noticed after commenting on this answer, slices in Python 3 return shallow copies of whatever they're slicing rather than views. Why is this still the case? Even leaving aside numpy's ...
35
votes
6answers
38k views
What is the correct syntax for 'else if'?
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1. Everything has gone fine until I tried to use the 'else if' statement. The interpreter gives me a syntax error after the 'if' in ...
34
votes
4answers
12k views
Hello World in Python
I am trying to learn Python, however I tried to run a script that is LITERALLY just:
print "Hello, World!"
And I get this error:
File "hello.py", line 1
print "Hello, World!"
...
32
votes
4answers
5k views
What are good uses for Python3's “Function Annotations”
Function Annotations: PEP-3107
I ran across a snippet of code demonstrating Python3's function annotations. The concept is simple but I can't think of why these were implemented in Python3 or any ...