草庐IT

instance-eval

全部标签

python - 在 Python、MATLAB 等中使用 eval

这个问题在这里已经有了答案:Whyisusing'eval'abadpractice?(8个答案)关闭9年前。我知道不应该使用eval。出于所有显而易见的原因(性能、可维护性等)。我的问题更多-它有合法用途吗?应该在哪里使用它而不是以另一种方式实现代码。由于它以多种语言实现并且可能导致糟糕的编程风格,我认为它仍然可用是有原因的。

python - 如何使用 tf.estimator 返回预测和标签(使用 predict 或 eval 方法)?

我正在使用Tensorflow1.4。我创建了一个自定义的tf.estimator来进行分类,如下所示:defmodel_fn():#Someoperationshere[...]returntf.estimator.EstimatorSpec(mode=mode,predictions={"Preds":predictions},loss=cost,train_op=loss,eval_metric_ops=eval_metric_ops,training_hooks=[summary_hook])my_estimator=tf.estimator.Estimator(model_f

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 - Python中exec和eval的使用

所以我明白了exec和eval以及compile的作用。但为什么我需要使用它们?我不清楚使用场景。任何人都可以给我一些例子,以便我更好地理解这个概念。因为我知道这都是理论。 最佳答案 我将给出一个示例,其中我使用了eval并且我认为它是最佳选择。我正在编写一个简单的软件测试实用程序……用来测试学生的练习是否符合作业要求。目标是为一个简单的配置文件提供一种方法作为测试规范(解决使用编程语言描述/记录/实现基本编程任务的测试用例的“先有鸡还是先有蛋”的问题).我的工具基于标准库中的ConfigParser。但是,我确实希望能够表示任意P

Python-ConfigParser-AttributeError : ConfigParser instance has no attribute '__getitem__'

我正在创建每日报价服务器。我正在阅读INI文件中的选项,其文本如下:[Server]host=port=17[Quotes]file=quotes.txt但是,当我使用ConfigParser时,它给我这个错误:Traceback(mostrecentcalllast):File"server.py",line59,inStart()File"server.py",line55,inStartconfigOptions=parseConfig(filename)File"server.py",line33,inparseConfigserver=config['Server']Attr

python - 为什么 ast.literal_eval ('5 * 7' ) 失败了?

为什么5*7的文字计算失败,而5+7却没有?importastprint(ast.literal_eval('5+7'))#->12print(ast.literal_eval('5*7'))#->Traceback(mostrecentcalllast):...ValueError:malformednodeorstring:documentation不解释这个。我在SO上回答了这个问题后发现了这个问题:Gettingtheresultofastring. 最佳答案 ast.literal_eval()接受评估数据中的+因为5+2

python - 在 python 3 中的函数中创建动态命名变量/理解 python 3 中的 exec/eval/locals

首先,我要说的是,我阅读了很多关于创建动态命名变量的类似主题的帖子,但它们大多与Python2相关,或者它们假定您正在使用类。是的,我读了BehaviorofexecfunctioninPython2andPython3.我也知道创建动态命名的变量在99%的时间里是一个坏主意,字典是获得的方式,但我只想知道它是否仍然可能以及exec和locals在python中的工作方式3.我想展示一些示例代码来说明我的问题(fibonacci计算斐波那契数,ListOfLetters提供["A","B",...]):deffunctionname():forindex,buchstabeinenum

python - type(instance) 何时不同于 instance.__class__?

Python有内置函数type:classtype(object)Withoneargument,returnthetypeofanobject.Thereturnvalueisatypeobjectandgenerallythesameobjectasreturnedbyobject.__class__.Python也有特殊属性__class__:instance.__class__Theclasstowhichaclassinstancebelongs.我曾经认为它们指的是同一个对象。然而在方法abc.ABCMeta.__instancecheck__检查它们是否相同:def__i

python - SQLAlchemy - ObjectDeletedError : Instance '<Class at...>' has been deleted. 帮助

我在从数据库中删除行然后添加新行时遇到了一些问题。这是代码:forpositionidinform_result['responsibilities']:inputdata=form_result['responsibilities'][positionid]self.__deleterow(dbmyaccount.Responsibilities,session['authed']['userid'])forrespin(i.strip()foriininputdata.split(',')):resp_q=dbmyaccount.Responsibilities(session['

python - 属性错误 : 'unicode' object has no attribute '_sa_instance_state'

我正在学习如何使用SQLAlchemy。我正在尝试执行以下操作,但将标题和链接存储在两个单独的表中:temp=Submissions(title=u'FacebookHomepage',link=u'http://facebook.com')session.add(temp)session.flush()transaction.commit()通过:classLinks(Base):__tablename__='links'id=Column(Integer,primary_key=True)link=Column(Text)created=Column(TIMESTAMP(),def