草庐IT

variable-initialization

全部标签

python - 执行类定义中的语句 : Which variables does the interpreter know about?

下面是我的部分类定义:classTrial:font=pygame.font.Font(None,font_size)target_dic={let:font.render(let,True,WHITE,BG)forletinlist("ABCDEFGHJKLMNPRSTUVWX")}部分类定义的最后一行,target_dic={let:font.render(let,True,WHITE,BG)forletinlist("ABCDEFGHJKLMNPRSTUVWX")返回错误:global名称“字体”未定义。很公平。然而,我尝试了以下测试用例并没有出现错误:classx:dat=1d

python - 这个 Python "static variable"hack 可以使用吗?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭6年前。Improvethisquestion一个经常被问到的问题是在Python中函数内部是否有一个等价于静态变量的东西。答案有很多,比如创建包装类、使用嵌套函数、装饰器等。我找到的最优雅的解决方案之一是this,我稍作修改:deffoo():#seeiffoo.counteralreadyexiststry:test=foo.counter#ifnot,initializeittowhateverexceptAttributeEr

python - 值错误 : Attempt to reuse RNNCell with a different variable scope than its first use

以下代码片段importtensorflowastffromtensorflow.contribimportrnnhidden_size=100batch_size=100num_steps=100num_layers=100is_training=Truekeep_prob=0.4input_data=tf.placeholder(tf.float32,[batch_size,num_steps])lstm_cell=rnn.BasicLSTMCell(hidden_size,forget_bias=0.0,state_is_tuple=True)ifis_trainingandke

python - 在 C 中嵌入 Python,链接失败, undefined reference `Py_Initialize'

我正在尝试编译文档中的示例https://docs.python.org/2.7/extending/embedding.html我的代码看起来和5.1下的完全一样:#includeintmain(intargc,char*argv[]){Py_SetProgramName(argv[0]);Py_Initialize();PyRun_SimpleString("fromtimeimporttime,ctime\n""print'Todayis',ctime(time())\n");Py_Finalize();return0;}我使用以下命令对其进行编译,这对我来说效果很好,并为我提供

python - "UnboundLocalError: local variable referenced before assignment"在函数中递增变量时

这个问题在这里已经有了答案:Usingglobalvariablesinafunction(24个答案)关闭9年前。我收到这个错误,我读过其他帖子,但他们说将global放在dollars=0之前,这会产生语法错误,因为它不允许=0。我将dollars用作计数器,这样我就可以跟踪添加到其中的内容并在需要时显示出来。dollars=0defsol():print('SearchorLeave?')sol=input()ifsol=='Search':search()ifsol=='Leave':leave()defsearch():print('Yougain5bucks')dollar

python - pyspark错误does not exist in jvm error when initializing SparkContext

我在emr上使用spark并编写pyspark脚本,尝试时出现错误frompysparkimportSparkContextsc=SparkContext()这是错误File"pyex.py",line5,insc=SparkContext()File"/usr/local/lib/python3.4/site-packages/pyspark/context.py",line118,in__init__conf,jsc,profiler_cls)File"/usr/local/lib/python3.4/site-packages/pyspark/context.py",line19

python - Django/djcelery 1.8.2 AppRegistryNotReady : Translation infrastructure cannot be initialized

我收到以下错误:File"/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/utils/translation/trans_real.py",line164,in_add_installed_apps_translations"Thetranslationinfrastructurecannotbeinitializedbeforethe"django.core.exceptions.AppRegistryNotReady:Thetranslationinfrastructurecannotbeinitial

python - 如何调试 "Exception while resolving variable in template ' 未知'”?

我一直在看DEBUGExceptionwhileresolvingvariable'exception_type'intemplate'unknown'.在我的django日志中,然后是VariableDoesNotExist:Failedlookupforkey[exception_type]in后跟看起来像是包含请求的字典列表的字符串表示形式,以及我的整个settings.py文件。另一个例子:DEBUGExceptionwhileresolvingvariable'lastframe'intemplate'unknown'我觉得我只是没有足够的信息来调试它。我所知道的是未知模板中

python - "ValueError: Trying to share variable $var, but specified dtype float32 and found dtype float64_ref"尝试使用 get_variable 时

我正在尝试构建自定义变分自动编码器网络,其中我使用来自编码器层的权重转置来初始化解码器权重,我找不到tf.contrib.layers的原生内容.fully_connected所以我使用了tf.assign,这是我的层代码:definference_network(inputs,hidden_units,n_outputs):"""Layerdefinitionfortheencoderlayer."""net=inputswithtf.variable_scope('inference_network',reuse=tf.AUTO_REUSE):forlayer_idx,hidden

python - 使用 vtk 时导入的 undefined variable

我使用与Ubuntu包管理器一起安装的python-vtk6.2.0在python2.7中编码。我使用eclipse+pydev作为IDE,我可以在shell和eclipse中成功运行代码。我遇到的唯一烦人的问题是代码分析器不断发布以下类型的错误:Undefinedvariablefromimport:vtkxxxxxx每当我这样做时importvtkvtk.vtkTransformPolyDataFiltervtk.vtkActorvtk.vtkWhatever我尝试重置解释器,我也尝试手动将库.so文件添加到PYTHONPATH,但没有成功。我还可以在系统库下的项目树中看到vtk包