What is the difference between dict.items() and dict.iteritems() in Python 2?

It’s part of an evolution. Originally, Python items() built a real list of tuples and returned that. That could potentially take a lot of extra memory. Then, generators were introduced to the language in general, and that method was reimplemented as an iterator-generator method named iteritems(). The original remains for backwards compatibility. One of Python … Read more

How do convert unicode escape sequences to unicode characters in a python string

Assuming Python sees the name as a normal string, you’ll first have to decode it to unicode: >>> name ‘Christensen Sk\xf6ld’ >>> unicode(name, ‘latin-1′) u’Christensen Sk\xf6ld’ Another way of achieving this: >>> name.decode(‘latin-1′) u’Christensen Sk\xf6ld’ Note the “u” in front of the string, signalling it is uncode. If you print this, the accented letter is … Read more

In Python, None evaluates to less than zero? [duplicate]

See the manual: Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). and CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same … Read more

Run interactive Bash with popen and a dedicated TTY Python

This is a solution to run an interactive command in subprocess. It uses pseudo-terminal to make stdout non-blocking(also some command needs a tty device, eg. bash). it uses select to handle input and ouput to the subprocess. #!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import select import termios import tty import … Read more

How can I optimize this Python code to generate all words with word-distance 1?

If your wordlist is very long, might it be more efficient to generate all possible 1-letter-differences from ‘word’, then check which ones are in the list? I don’t know any Python but there should be a suitable data structure for the wordlist allowing for log-time lookups. I suggest this because if your words are reasonable … Read more

Is it ever useful to use Python’s input over raw_input?

Is it ever useful to use Python 2’s input over raw_input? No. input() evaluates the code the user gives it. It puts the full power of Python in the hands of the user. With generator expressions/list comprehensions, __import__, and the if/else operators, literally anything Python can do can be achieved with a single expression. Malicious … Read more

isinstance() and issubclass() return conflicting results

The accepted answer is correct, but seems to miss an important point. The built-in functions isinstance and issubclass ask two different questions. isinstance(object, classinfo) asks whether an object is an instance of a class (or a tuple of classes). issubclass(class, classinfo) asks whether one class is a subclass of another class (or other classes). In … Read more

Relationship between pickle and deepcopy

You should not be confused by (1) and (2). In general, Python tries to include sensible fall-backs for missing methods. (For instance, it is enough to define __getitem__ in order to have an iterable class, but it may be more efficient to also implement __iter__. Similar for operations like __add__, with optional __iadd__ etc.) __deepcopy__ … Read more

tech