python-2.x

Python's handling of shell strings

南笙酒味 提交于 2019-12-04 09:31:05
I still do not understand completely how python's unicode and str types work. Note: I am working in Python 2, as far as I know Python 3 has a completely different approach to the same issue. What I know : str is an older beast that saves strings encoded by one of the way too many encodings that history has forced us to work with. unicode is an more standardised way of representing strings using a huge table of all possible characters, emojis, little pictures of dog poop and so on. The decode function transforms strings to unicode, encode does the other way around. If I, in python's shell,

How do I tell dict() in Python 2 to use unicode instead of byte string?

為{幸葍}努か 提交于 2019-12-04 07:48:33
Here is an example: d = dict(a = 2) print d {'a': 2} How can I tell dict() constructor to use Unicode instead without writing the string literal expliclity like u'a' ? I am loading a dictionary from a json module which defaults to use unicode. I want to make use of unicode from now on. To get a dict with Unicode keys, use Unicode strings when constructing the dict: >>> d = {u'a': 2} >>> d {u'a': 2} Dicts created from keyword arguments always have string keys. If you want those to be Unicode (as well as all other strings), switch to Python 3. Keyword arguments in 2.x can only use ASCII

What is a good way to layer like this in pygame?

余生颓废 提交于 2019-12-04 05:20:04
问题 I've been working on a small project in pygame. It's an overhead 2D game, but if a player is behind an object, I need the player to be one layer behind it. If the player is in front of the object, it needs to be a layer ahead of it. for image, imagerect, imageypos in zip(blitimages, blitrects, blitypositions): if realy < imageypos: screen.blit(playerimage, playerimagerect) screen.blit(image, imagerect) if realy > imageypos: screen.blit(image, imagerect) screen.blit(playerimage,

TypeError: 'tuple' object does not support item assignment

筅森魡賤 提交于 2019-12-04 05:05:12
问题 I'm trying to write a short program which allows the user to input a list of numbers into an input() function, and then using the add_25 function add 25 to each item in a list. I get the following error when the program runs: TypeError: 'tuple' object does not support item assignment I tried dividing the numbers using a comma. This is the program: testlist = [2,6,2] def add_25(mylist): for i in range(0, len(mylist)): mylist[i] = mylist[i] + 25 return mylist print add_25(testlist) actual_list

Invalid command name while executing (“after” script)

筅森魡賤 提交于 2019-12-04 04:41:59
问题 As solve this problem? I'm running this code, window is created, but in console appears message on the error. I think problem the fact that is "after" loop not terminate but the window already destroyed. Code: import Tkinter as tk import time class App(): def __init__(self): self.root = tk.Tk() self.label = tk.Label(text="") self.label.pack() self.update_clock() self.root.mainloop() def update_clock(self): now = time.strftime("%H:%M:%S") self.label.configure(text=now) self.root.after(1000,

What is the Python <> operator

*爱你&永不变心* 提交于 2019-12-04 04:23:56
问题 What exactly is the <> operator in Python, and why is it undocumented (as far as I can tell)? Is it the same as != or is not ? 回答1: In Python 2.x, <> is the same as != (i.e. "not equal to" , rather than is not which is "not identical to" ), but the latter is preferred: The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent. In 3.x, <> has been removed and only != exists. 回答2: It is documented, but you're not supposed to

Why is there a performance difference between the order of a nested loop?

有些话、适合烂在心里 提交于 2019-12-04 04:22:37
问题 I have a process that loops through two lists, one being relatively large while the other being significantly smaller. Example: larger_list = list(range(15000)) smaller_list = list(range(2500)) for ll in larger_list: for sl in smaller_list: pass I scaled the sized down of the lists to test performance, and I noticed there is a decent difference between which list is looped through first. import timeit larger_list = list(range(150)) smaller_list = list(range(25)) def large_then_small(): for ll

Closure in python?

浪子不回头ぞ 提交于 2019-12-04 03:47:41
问题 When I run this code, I get this result: 15 15 I expect the output should be 15 17 but it is not. The question is: why? def make_adder_and_setter(x): def setter(n): x = n return (lambda y: x + y, setter) myadder, mysetter = make_adder_and_setter(5) print myadder(10) mysetter(7) print myadder(10) 回答1: Python 2.x has a syntax limitation that doesn't allow to capture a variable in read/write. The reason is that if a variable is assigned in a function there are only two possibilities: the

Modify URL components in Python 2

懵懂的女人 提交于 2019-12-04 03:36:28
Is there a cleaner way to modify some parts of a URL in Python 2? For example http://foo/bar -> http://foo/yah At present, I'm doing this: import urlparse url = 'http://foo/bar' # Modify path component of URL from 'bar' to 'yah' # Use nasty convert-to-list hack due to urlparse.ParseResult being immutable parts = list(urlparse.urlparse(url)) parts[2] = 'yah' url = urlparse.urlunparse(parts) Is there a cleaner solution? Martijn Pieters Unfortunately, the documentation is out of date; the results produced by urlparse.urlparse() (and urlparse.urlsplit() ) use a collections.namedtuple() -produced

How to convert hexadecimal string to character with that code point?

好久不见. 提交于 2019-12-04 03:34:09
问题 I have the string x = '0x32' and would like to turn it into y = '\x32' . Note that len(x) == 4 and len(y) == 1 . I've tried to use z = x.replace("0", "\\") , but that causes z = '\\x32' and len(z) == 4 . How can I achieve this? 回答1: You do not have to make it that hard: you can use int(..,16) to parse a hex string of the form 0x... . Next you simply use chr(..) to convert that number into a character with that Unicode (and in case the code is less than 128 ASCII) code: y = chr(int(x,16)) This