在现代Python中声明自定义异常类的正确方法是什么?我的主要目标是遵循标准的其他异常类,因此(例如)我在异常中包含的任何额外字符串都会被捕获异常的任何工具打印出来.
通过"现代Python",我指的是将在Python 2.5中运行的东西,但对于Python 2.6和Python 3*的处理方式来说是"正确的".而"自定义"我指的是一个Exception对象,它可以包含有关错误原因的额外数据:一个字符串,也许还有一些与异常相关的任意对象.
我被Python 2.6.2中的以下弃用警告绊倒了:
>>> class MyError(Exception):
... def __init__(self, message):
... self.message = message
...
>>> MyError("foo")
_sandbox.py:3: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
Run Code Online (Sandbox Code Playgroud)
看起来很疯狂,BaseException对于名为的属性具有特殊含义message.我从PEP-352收集到该属性确实在2.5中有特殊含义他们试图弃用,所以我猜这个名字(而且仅此一个)现在被禁止了?啊.
我也模糊地意识到它Exception有一些神奇的参数args,但我从来不知道如何使用它.我也不确定这是向前发展的正确方法; 我在网上发现的很多讨论都表明他们试图在Python 3中废除args.
更新:两个答案建议覆盖__init__,和__str__/ __unicode__/ __repr__.这似乎很多打字,是否有必要?
我有四个不同的文件:main,vector,entity和physics.我不会发布所有代码,只发布导入,因为我认为这就是错误所在.(如果你愿意,我可以发布更多)
主要:
import time
from entity import Ent
from vector import Vect
#the rest just creates an entity and prints the result of movement
Run Code Online (Sandbox Code Playgroud)
实体:
from vector import Vect
from physics import Physics
class Ent:
#holds vector information and id
def tick(self, dt):
#this is where physics changes the velocity and position vectors
Run Code Online (Sandbox Code Playgroud)
向量:
from math import *
class Vect:
#holds i, j, k, and does vector math
Run Code Online (Sandbox Code Playgroud)
物理:
from entity import Ent
class Physics:
#physics class gets an entity …Run Code Online (Sandbox Code Playgroud) 我有一个我在TypeScript中创建的数组,它有一个我用作键的属性.如果我有该密钥,我该如何从中删除一个项目?
是否可以在方法中使用多个@RequestMapping spring注释?喜欢:
@RequestMapping("/")
@RequestMapping("")
@RequestMapping("/welcome")
public String welcomeHandler(){
return("welcome");
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种简单(快速)的方法来确定两个无序列表是否包含相同的元素:
例如:
['one', 'two', 'three'] == ['one', 'two', 'three'] : true
['one', 'two', 'three'] == ['one', 'three', 'two'] : true
['one', 'two', 'three'] == ['one', 'two', 'three', 'three'] : false
['one', 'two', 'three'] == ['one', 'two', 'three', 'four'] : false
['one', 'two', 'three'] == ['one', 'two', 'four'] : false
['one', 'two', 'three'] == ['one'] : false
Run Code Online (Sandbox Code Playgroud)
我希望不使用地图就能做到这一点.
已经有一段时间了,我无法绕过我想要制作的算法.基本上,我有两个列表,并希望获得两个列表的所有组合.
我可能不会解释它是正确的,所以这是一个例子.
name = 'a', 'b'
number = 1, 2
Run Code Online (Sandbox Code Playgroud)
在这种情况下的输出将是:
1. A1 B2
2. B1 A2
Run Code Online (Sandbox Code Playgroud)
棘手的部分是"name"变量中的项目可能比"number"变量中的项目更多(数字将始终等于或小于name变量).
我很困惑如何进行所有组合(嵌套for循环?),如果名称中的项目多于它们在数字列表中的项目,则更加混淆逻辑以移动名称变量中的项目.
我不是最好的程序员但是如果有人可以帮助我澄清逻辑/算法来实现这一点,我想我可以试一试.所以我刚刚陷入嵌套for循环.
更新:
这是3个变量和2个数字的输出:
name = 'a', 'b', 'c'
number = 1, 2
Run Code Online (Sandbox Code Playgroud)
输出:
1. A1 B2
2. B1 A2
3. A1 C2
4. C1 A2
5. B1 C2
6. C1 B2
Run Code Online (Sandbox Code Playgroud) 我一直在看杰克逊,但似乎我必须将Map转换为JSON,然后将结果JSON转换为POJO.
有没有办法将Map直接转换为POJO?
我正在尝试在Python中创建一组集合.我无法弄清楚该怎么做.
从空集开始xx:
xx = set([])
# Now we have some other set, for example
elements = set([2,3,4])
xx.add(elements)
Run Code Online (Sandbox Code Playgroud)
但我明白了
TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)
要么
TypeError: unhashable type: 'set'
Run Code Online (Sandbox Code Playgroud)
是否可以在Python中拥有一组集合?
我正在处理大量的集合,我希望能够不必处理重复的集合(集合A1,A2,......的集合B,如果Ai = Aj,则会"取消"两个集合)
我想得到一个类的属性,比如说:
class MyClass():
a = "12"
b = "34"
def myfunc(self):
return self.a
Run Code Online (Sandbox Code Playgroud)
使用MyClass.__dict__给我一个属性和功能的列表,甚至像__module__和的功能__doc__.虽然MyClass().__dict__我给了一个空的dict,除非我明确设置了该实例的属性值.
我只想要属性,在上面的例子中,那些将是:a和b
Python的单元测试框架中的assertAlmostEqual(x,y)方法测试是否和大致相等,假设它们是浮点数.xy
问题assertAlmostEqual()是它只适用于浮点数.我正在寻找一种方法,如assertAlmostEqual()浮点数,浮点数集,浮点字典,浮点元组,浮点元组列表,浮点列表等等.
例如,让x = 0.1234567890,y = 0.1234567891.x并且y几乎相同,因为他们同意除最后一个数字之外的每个数字.因此self.assertAlmostEqual(x, y)是True因为assertAlmostEqual()花车的作品.
我正在寻找一个更通用的assertAlmostEquals(),也评估以下调用True:
self.assertAlmostEqual_generic([x, x, x], [y, y, y]).self.assertAlmostEqual_generic({1: x, 2: x, 3: x}, {1: y, 2: y, 3: y}).self.assertAlmostEqual_generic([(x,x)], [(y,y)]).有这样的方法还是我必须自己实施?
澄清:
assertAlmostEquals()有一个名为的可选参数places,通过计算舍入到十进制数的差值来比较数字places.默认情况下places=7,因此self.assertAlmostEqual(0.5, 0.4)为False,而self.assertAlmostEqual(0.12345678, 0.12345679)为True.我的推测assertAlmostEqual_generic()应该具有相同的功能.
如果两个列表在完全相同的顺序中具有几乎相等的数字,则认为它们几乎相等.正式地,for i …
python ×7
collections ×3
list ×2
algorithm ×1
arrays ×1
attributes ×1
class ×1
combinations ×1
comparison ×1
dictionary ×1
exception ×1
importerror ×1
jackson ×1
java ×1
json ×1
nested ×1
pojo ×1
python-2.7 ×1
set ×1
spring ×1
spring-mvc ×1
typescript ×1
unit-testing ×1
url-routing ×1