How do you test that a Python function throws an exception?

如何编写一个单元测试,只有在函数没有抛出预期的异常时才会失败? 使用unittest模块中的TestCase.assertRaises (或TestCase.failUnlessRaises ),例如: import mymod class MyTestCase(unittest.TestCase): def test1(self): self.assertRaises(SomeCoolException, mymod.myfunc) Since Python 2.7 you can use context manager to get a hold of the actual Exception object thrown: import unittest def br

你如何测试一个Python函数抛出异常?

如何编写一个单元测试,只有在函数没有抛出预期的异常时才会失败? 使用unittest模块中的TestCase.assertRaises (或TestCase.failUnlessRaises ),例如: import mymod class MyTestCase(unittest.TestCase): def test1(self): self.assertRaises(SomeCoolException, mymod.myfunc) 自Python 2.7以来,您可以使用上下文管理器来获取抛出的实际Exception对象: import unittest def broken_function(): raise

How to make a custom exception class with multiple init args pickleable

Why does my custom Exception class below not serialize/unserialize correctly using the pickle module? import pickle class MyException(Exception): def __init__(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2 super(MyException, self).__init__(arg1) e = MyException("foo", "bar") str = pickle.dumps(e) obj = pickle.loads(str) This code throws the following error:

如何使用多个初始化参数创建自定义异常类pickleable

为什么我的自定义Exception类不能使用pickle模块正确序列化/反序列化? import pickle class MyException(Exception): def __init__(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2 super(MyException, self).__init__(arg1) e = MyException("foo", "bar") str = pickle.dumps(e) obj = pickle.loads(str) 此代码引发以下错误: Traceback (most recent call last): File "test.py",

Passing python dictionary to a class changes the value of dictionary

This question already has an answer here: How do I pass a variable by reference? 23 answers 一般来说,您需要将字典的副本传递给您的班级 - 或者您可以在班内复印一份副本...... self.myDict = myDict.copy()

将Python字典传递给类会改变字典的值

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 一般来说,您需要将字典的副本传递给您的班级 - 或者您可以在班内复印一份副本...... self.myDict = myDict.copy()

Variable name as function argument?

This question already has an answer here: How do I pass a variable by reference? 23 answers Have the rounding function return a value. def rounding(list_): return [round(i, 1) for i in list_] Then you can do this: >>> morada=[1,2.2342,4.32423,6.1231] #an easy example >>> morada = rounding(morada) >>> morada [1, 2.2, 4.3, 6.1] Or if you really really wanted

变量名称作为函数参数?

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 rounding函数return一个值。 def rounding(list_): return [round(i, 1) for i in list_] 然后你可以这样做: >>> morada=[1,2.2342,4.32423,6.1231] #an easy example >>> morada = rounding(morada) >>> morada [1, 2.2, 4.3, 6.1] 或者如果你真的想让它在函数内分配,你可以这样做: def rounding(list_): list_

Send by ref/by ptr in python?

This question already has an answer here: How do I pass a variable by reference? 23 answers In some ways, all calls in Python are called with references. In fact, all variables are references in a sense. But some types, like int from your example, cannot be changed. In the case of, say, a list , the functionality you're looking for is trivial: def change_it(some_list): some_list

在python中通过ref / by ptr发送?

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 在某些方面,Python中的所有调用都是通过引用调用的。 事实上,从某种意义上说,所有变量都是引用。 但是有些类型,比如你的例子中的int ,不能改变。 例如,在list ,您要查找的功能很简单: def change_it(some_list): some_list.append("world") foo = ["hello"] change_it(foo) print(foo) # prints ['hello', 'world'] 但请注意,重新分配

how to assign variable by reference in python?

This question already has an answer here: How do I pass a variable by reference? 23 answers The simple answer is that all variables in python are references. Some references are to immutable objects (such as strings, integers etc), and some references are to mutable objects (lists, sets). There is a subtle difference between changing a referenced object's value (such as adding to an ex

如何在python中通过引用分配变量?

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 简单的答案是python中的所有变量都是引用。 一些引用是不可变对象(如字符串,整数等),一些引用是可变对象(列表,集合)。 改变引用对象的值(如添加到现有列表)和更改引用(更改变量以引用完全不同的列表)之间存在细微差别。 在你的例子中,你最初是x = oa使变量x成为任何oa的引用(对1的引用)。 当你然后做x = 2 ,你所作出的修改X引用(它现在

How to change the scope of a variable in a function? Python

This question already has an answer here: How do I pass a variable by reference? 23 answers In Python, why can a function modify some arguments as perceived by the caller, but not others? 9 answers Think of them as being part of the function. When the function ends, all its variables die too. x=2 y=3 def func(x,y): x=200 y=300 func(x,y) #inside this function, x=200 and y=300 #

如何更改函数中变量的范围? 蟒蛇

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 在Python中,为什么函数可以修改调用者感知的某些参数,而不是其他参数? 9个答案 将它们视为功能的一部分。 当函数结束时,它的所有变量也会死掉。 x=2 y=3 def func(x,y): x=200 y=300 func(x,y) #inside this function, x=200 and y=300 #but by this line the function is over and those new values are discarded print(x,y) #so this i

How does python assign values after assignment operator

This question already has an answer here: How do I pass a variable by reference? 23 answers I've faced this problem before and understand that it gets confusing. There are two concepts here: some data structures are mutable, while others are not Python works off pointers... most of the time So let's consider the case of a list (you accidentally stumbled on interning and peepho

python如何在赋值运算符后赋值

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 我以前遇到过这个问题,并且明白它会让人困惑。 这里有两个概念: 一些数据结构是可变的,而另一些则不是 Python在大多数情况下都是关闭指针的 所以让我们考虑一下列表的情况(当您使用ints时,您意外地发现了实习和窥视孔优化 - 我会在稍后讨论) 所以让我们创建两个相同的列表(记住列表是可变的) In [42]: a = [1,2] In [43]: b = [1,2] In [

Modify parameter as a side

Possible Duplicate: Python: How do I pass a variable by reference? I'm trying to write a function that modifies one of the passed parameters. Here's the current code: def improve_guess(guess, num): return (guess + (num/guess)) / 2 x = 4.0 guess = 2.0 guess = improve_guess(guess, x) However, I want to write the code in such a way that I don't have to do the final assignment.

修改参数作为一面

可能重复: Python:我如何通过引用传递变量? 我试图编写一个函数来修改传递的参数之一。 这是当前的代码: def improve_guess(guess, num): return (guess + (num/guess)) / 2 x = 4.0 guess = 2.0 guess = improve_guess(guess, x) 但是,我想以这样的方式编写代码,我不必做最后的任务。 这样,我可以打电话给: improve_guess(guess,x) 并猜测新的价值。 (我故意没有提到通过引用,因为在我的网络搜索过

Without pointers, can I pass references as arguments in Python?

This question already has an answer here: How do I pass a variable by reference? 23 answers Your understanding is, unfortunately, completely wrong. Python does not copy the value, nor does it allocate space for a new one. It passes a value which is itself a reference to the object. If you modify that object (rather than rebinding its name), then the original will be modified. Edit I w

如果没有指针,我可以将引用作为Python中的参数传递吗?

这个问题在这里已经有了答案: 如何通过引用传递变量? 23个答案 不幸的是,你的理解完全错误。 Python不会复制值,也不会为新值分配空间。 它传递一个本身是对象引用的值。 如果您修改该对象(而不是重新命名它的名称),那么原件将被修改。 编辑 我希望你不再担心内存分配问题:Python不是C ++,几乎所有的时候你都不需要考虑内存。 通过使用类似列表的方式演示重新绑定更容易: def my_func(foo): foo.appe