草庐IT

global-scope

全部标签

Python 的 eval() 和 globals()

我正在尝试使用eval()执行一些函数,我需要为它们创建某种运行环境。文档中说您可以将全局变量作为第二个参数传递给eval()。但在我的情况下似乎不起作用。这是简化的示例(我尝试了两种方法,声明变量全局和使用globals(),但两者都不起作用):文件script.py:importtestglobaltest_variabletest_variable='test_value'g=globals()g['test_variable']='test_value'eval('test.my_func()',g)文件test.py:defmy_func():globaltest_varia

python - 使用 dict comprehensions 时的问题。名称错误 : global name is not defined

我正在尝试创建一个字典,其键为name,值为对应的User对象。我正在使用来自Djangoshell包装器pythonmanage.pyshell的Pythonshell:>>>fromdjango.contrib.auth.modelsimportUser>>>names=['carl','jim','jack','john','mark']#Nowusingsomedictcomprehension>>>u={name:User.objects.get(username=name)fornameinnames}NameError:globalname'User'isnotdefin

python - TensorFlow 2.0 : how to group graph using tf. 喀拉斯? tf.name_scope/tf.variable_scope 不再使用了吗?

回到TensorFlowinception模块,通过使用tf.name_scope或tf将它们分组.variable_scope.利用这些运算符,我们能够方便地构造计算图,从而使TensorBoard的图View更容易解释。只是结构化组的一个例子:这对于调试复杂的架构非常方便。不幸的是,tf.keras似乎忽略了tf.name_scope并且tf.variable_scope在TensorFlow>=2.0中消失了。因此,像这样的解决方案......withtf.variable_scope("foo"):withtf.variable_scope("bar"):v=tf.get_va

python - Python __import__ 函数中的 `globals` 和 `locals` 参数有什么用?

有一部分__import__在Python文档中,我不明白:__import__(name[,globals[,locals[,fromlist[,level]]]])Thefunctionimportsthemodulename,potentiallyusingthegivenglobalsandlocalstodeterminehowtointerpretthenameinapackagecontext.Thestandardimplementationdoesnotuseitslocalsargumentatall,andusesitsglobalsonlytodetermine

python - 谷歌 API Python unauthorized_client : Unauthorized client or scope in request

尝试运行我的代码时出现此错误:oauth2client.client.AccessTokenRefreshError:unauthorized_client:Unauthorizedclientorscopeinrequest.这是我的代码:importjsonimportrequestsimporthttplib2fromoauth2client.clientimportSignedJwtAssertionCredentialsfromapiclient.discoveryimportbuildif__name__=='__main__':json_key_file='my-key.

没有参数的 Python 'raise' : what is "the last exception that was active in the current scope"?

Python的文档说:Ifnoexpressionsarepresent,raisere-raisesthelastexceptionthatwasactiveinthecurrentscope.(Python3:https://docs.python.org/3/reference/simple_stmts.html#raise;Python2.7:https://docs.python.org/2.7/reference/simple_stmts.html#raise。)但是,“最后激活”的概念似乎已经改变。见证以下代码示例:#from__future__importprint_f

python - 使用列表理解来查找变量适用于 globals() 但不适用于 locals()。为什么?

这个问题在这里已经有了答案:Pythonscopingindictcomprehension(1个回答)Pythondictionarycomprehensionusinglocals()givesKeyError(2个答案)Subscriptinglocals()inadictcomprehensionfailswithKeyError[duplicate](1个回答)关闭4年前。我正在将项目从python2.7更新到python3.6。我有一个列表理解,可以从在python2.7中工作的本地变量中查找变量。当我切换到使用全局变量时,它仅适用于python3.6。下面是一个玩具示例来

python - locals() 和 globals() 以及 python 中的 dir() 之间的区别

假设这段代码:>>>iterator=filter(lambdax:x%3==0,[2,18,9,22,17,24,8,12,27])>>>x=int()>>>locals(){'__package__':None,'__spec__':None,'__loader__':,'__name__':'__main__','__builtins__':,'iterator':,'x':0,'__doc__':None}>>>globals(){'__package__':None,'__spec__':None,'__loader__':,'__name__':'__main__','__

python - Python依赖 hell : A compromise between virtualenv and global dependencies?

到目前为止,我已经测试了多种方法来用在Python中管理我的项目依赖项:使用pip全局安装所有内容(节省空间,但迟早会给您带来麻烦)pip和venv或virtualenv(管理起来有点麻烦,但在许多情况下还可以)pipenv和pipfile(比venv/virtualenv容易一些,但速度较慢,并且有一些供应商锁定,虚拟环境隐藏在实际项目文件夹之外的其他位置)conda作为程序包和环境管理器(最好在conda中提供所有程序包,将pip和conda混合使用会有点麻烦)诗歌-我还没有尝试过这个...我所有这些问题(除了1.)的问题是我的硬盘空间很快就被填满了:我不是开发人员,我在日常工作中

python - tf.global_variables_initializer() 在幕后做了什么?

有很多案例(here和here)TensorFlow用户添加init_op=tf.global_variables_initializer()在定义任何变量或操作之前,然后按照以下行出现错误Attemptingtouseuninitializedvalue有解释here但它没有提及底层的tf.global_variables_initializer调用。它几乎是在批量复制TFAPI。本题侧重于部分用户调用sess.run(init_op)时,仍然存在未初始化的值。示例代码和对tf.global_variables_initializer的分析会很棒。 最佳答