我正在使用python 2.7.
我有以下课程:
class Test:
def __init__(self):
self._pos_a = ['test_1']
self._pos_b = ['test_2']
@property
def pos_a(self):
return self._pos_a
@property
def pos_b(self):
return self._pos_b
if __name__ == "__main__":
x = Test()
x.pos_a = True
print x.pos_a
>>> True
Run Code Online (Sandbox Code Playgroud)
我的理解是,通过使用属性装饰器,我基本上为我的两个类属性建立了一个getter方法.既然我没有创建一个setter方法,我希望我对x.pos_a的"True"赋值会引发错误.错误应该是我不能设置有getter方法但没有setter方法的属性的值.相反,该值设置为"True",它打印没有问题.我如何实现这一目标以实现这一结果?我希望用户能够"获取"这些值,但他们不应该设置它们.
我需要获取文件名字符串,并尝试打开该文件。如果找不到该文件,我将循环直到输入正确的字符串。
public static void main(String[] args){
// Get file string until valid input is entered.
System.out.println("Enter file name.\n Enter ';' to exit.");
String fileName = sc.nextLine();
boolean fileLoop = true;
InputStream inFile;
while (fileLoop){
try{
inFile = new FileInputStream(fileName);
fileLoop = false;
} catch (FileNotFoundException e) {
System.out.println("That file was not found.\n Please re enter file name.\n Enter ';' to exit.");
fileName = sc.nextLine();
if (fileName.equals(";")){
return;
}
}
}
// ****** This is where the error is. It says …Run Code Online (Sandbox Code Playgroud) 我有以下任务,我想通过多线程(python3)更快地完成.
import threading, time
q = []
def fill_list():
global q
while True:
q.append(1)
if len(q) >= 1000000000:
return
Run Code Online (Sandbox Code Playgroud)
第一个主要不使用多线程:
t1 = time.clock()
fill_list()
tend = time.clock() - t1
print(tend)
Run Code Online (Sandbox Code Playgroud)
结果是145秒的运行时间.
第二个调用两个线程:
t1 = time.clock()
thread1 = threading.Thread(target=fill_list, args=())
thread2 = threading.Thread(target=fill_list, args=())
thread1.start()
thread2.start()
thread1.join()
thread2.join()
tend = time.clock() - t1
print(tend)
Run Code Online (Sandbox Code Playgroud)
这需要152秒才能完成.
最后,我添加了第三个帖子.
t1 = time.clock()
thread1 = threading.Thread(target=fill_list, args=())
thread2 = threading.Thread(target=fill_list, args=())
thread3 = threading.Thread(target=fill_list, args=())
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
tend = time.clock() - …Run Code Online (Sandbox Code Playgroud) 为了说明我的问题,我将生成一个简单的热图.
import matplotlib.pyplot as plt
import numpy as np
heatmap = np.asarray([[20, 0, 40],
[0, 40, 40],
[20, 40, 50]])
heatmap_plot = plt.pcolor(heatmap, cmap='RdBu', vmin=0, vmax=100)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这产生了以下图像;
在这里,可以看到较低的值显示为较暗的红色,较大的值显示为白色.如何撤消此设置?换句话说,我如何使较低的值更浅的色调(0 =白色).较大的值表示较暗的颜色,在本例中为红色.
我似乎找不到任何这样的参数看pcolor文档.