python-2.x

python equality precedence

蹲街弑〆低调 提交于 2019-12-30 16:22:25
问题 class L(object): def __eq__(self, other): print 'invoked L.__eq__' return False class R(object): def __eq__(self, other): print 'invoked R.__eq__' return False left = L() right = R() With this code, left side gets the first shot at comparison, as documented in the data model: >>> left == right invoked L.__eq__ False But if we make a slight modification on line 6 (everything else the same): class R(L): Now the right side gets to have the first shot at comparison. >>> left == right invoked R._

Import some python3 modules in Python2

倾然丶 夕夏残阳落幕 提交于 2019-12-30 10:00:11
问题 Is there a way to import Python 3 modules into Python 2 scripts? I want to use some Python 3 modules in a Django application and haven't seen anything on the Internet. Any clues? 回答1: I don't think it's really possible, no. The same instance of the interpreter has to handle every module imported in a given app, so there's no obvious way to mix and match interpreters. If you need to accomplish a discrete task with a Python 3 module, you could try making a command-line script to accomplish your

How to return multiple values from *args?

99封情书 提交于 2019-12-30 08:29:08
问题 I have a hello function and it takes n arguments (see below code). def hello(*args): # return values I want to return multiple values from *args . How to do it? For example: d, e, f = hello(a, b, c) SOLUTION: def hello(*args): values = {} # values rst = [] # result for arg in args: rst.append(values[arg]) return rst a, b, c = hello('d', 'e', f) a, b = hello('d', 'f') Just return list. :) :D 回答1: So, you want to return a new tuple with the same length as args (i.e. len(args)), and whose values

Uses of Python's “from” keyword?

时光怂恿深爱的人放手 提交于 2019-12-30 08:03:24
问题 Are there any other uses for Python's "from" keyword aside from import statements? 回答1: No and yes. According to the official Python 2.7.2 grammar, the only occurrence of the word from is in the clause import_from , so no. In the Python 3.1.3 grammar a new clause raise_stmt: 'raise' [test ['from' test]] appears, so yes. 回答2: In Python 2.x, the only use of from is for the from x import y statement. However, for Python 3.x, it can be used in conjunction with the raise statement, e.g.: try:

Uses of Python's “from” keyword?

╄→尐↘猪︶ㄣ 提交于 2019-12-30 08:01:32
问题 Are there any other uses for Python's "from" keyword aside from import statements? 回答1: No and yes. According to the official Python 2.7.2 grammar, the only occurrence of the word from is in the clause import_from , so no. In the Python 3.1.3 grammar a new clause raise_stmt: 'raise' [test ['from' test]] appears, so yes. 回答2: In Python 2.x, the only use of from is for the from x import y statement. However, for Python 3.x, it can be used in conjunction with the raise statement, e.g.: try:

Integer division compared to floored quotient: why this surprising result?

前提是你 提交于 2019-12-30 06:14:14
问题 The // "integer division" operator of Python surprised me, today: >>> math.floor(11/1.1) 10.0 >>> 11//1.1 9.0 The documentation reads "(floored) quotient of x and y". So, why is math.floor(11/1.1) equal to 10, but 11//1.1 equal to 9? 回答1: Because 1.1 can't be represented in binary form exactly; the approximation is a littler higher than 1.1 - therefore the division result is a bit too small. Try the following: Under Python 2, type at the console: >>> 1.1 1.1000000000000001 In Python 3.1, the

Python argparse: command-line argument that can be either named or positional

本秂侑毒 提交于 2019-12-30 06:01:39
问题 I am trying to make a Python program that uses the argparse module to parse command-line options. I want to make an optional argument that can either be named or positional. For example, I want myScript --username=batman to do the same thing as myScript batman . I also want myScript without a username to be valid. Is this possible? If so, how can it be done? I tried various things similar to the code below without any success. parser = argparse.ArgumentParser() group = parser.add_mutually

Convert float to string in positional format (without scientific notation and false precision)

浪尽此生 提交于 2019-12-29 10:53:48
问题 I want to print some floating point numbers so that they're always written in decimal form (e.g. 12345000000000000000000.0 or 0.000000000000012345 , not in scientific notation, yet I'd want to the result to have the up to ~15.7 significant figures of a IEEE 754 double, and no more. What I want is ideally so that the result is the shortest string in positional decimal format that still results in the same value when converted to a float . It is well-known that the repr of a float is written in

Convert float to string in positional format (without scientific notation and false precision)

感情迁移 提交于 2019-12-29 10:53:15
问题 I want to print some floating point numbers so that they're always written in decimal form (e.g. 12345000000000000000000.0 or 0.000000000000012345 , not in scientific notation, yet I'd want to the result to have the up to ~15.7 significant figures of a IEEE 754 double, and no more. What I want is ideally so that the result is the shortest string in positional decimal format that still results in the same value when converted to a float . It is well-known that the repr of a float is written in

How to write Russian characters in file?

谁说我不能喝 提交于 2019-12-29 07:32:09
问题 In console when I'm trying output Russian characters It gives me ??????????????? Who know why? I tried write to file - in this case the same situation. for example f=open('tets.txt','w') f.write('some russian text') f.close inside file is - ?????????????????????????/ or p="some russian text" print p ????????????? In additional Notepad don't allow me to save file with Russian letters. I give this: This file contains characters in Unicode format which will be lost if you save this file as an