我在我的Deep Learning VM 中创建了一个 conda 环境。当我通过 ssh 连接到它(在 VM 实例页面中单击我的实例的 SSH 按钮)并键入source activate <environment_name>它时,它会在 shell 中正确激活。
我从我的本地机器成功连接到 jupyter 实验室,如文档中所述
如何在此 VM 的特定 conda 环境中使用 jupyter?
在特定的 conda 环境中运行 jupyter的公认方式似乎是
source activate <environment_name>在运行 jupyter notebook 之前使用 激活终端中的conda环境。
但是深度学习 VM 文档说
当您的深度学习虚拟机实例初始化时,Jupyter 实验室会话开始
这样我就无法在创建 jupyter 实验室会话之前激活源。
有任何想法吗 ?
我想在我的类中定义许多方法TestClass。我想呼唤他们的名字TestClass().method_1......TestClass().method_n
我不想间接调用它们,例如通过中间方法来TestClass().use_method('method_1', params)保持与代码其他部分的一致性。
我想动态定义我的众多方法,但我不明白为什么这个最小的示例不起作用:
class TestClass:
def __init__(self):
method_names = [
'method_1',
'method_2']
for method_name in method_names:
def _f():
print(method_name)
# set the method as attribute
# (that is OK for me that it will not be
# a bound method)
setattr(
self,
method_name,
_f)
del _f
if __name__ == '__main__':
T = TestClass()
T.method_1()
T.method_2()
print(T.method_1)
print(T.method_2)
Run Code Online (Sandbox Code Playgroud)
输出是:
function_2
function_2
<function TestClass.__init__.<locals>._f at 0x0000022ED8F46430>
<function TestClass.__init__.<locals>._f at 0x0000022EDADCE4C0>
Run Code Online (Sandbox Code Playgroud)
当我期待的时候
function_1
function_2
Run Code Online (Sandbox Code Playgroud)
我尝试在很多地方放置一些 …