在python中将字符串作为变量调用(calling string as variable in python)

我在python中创建了一个名为SignalParam的类,包含不同的属性(频率,电压,时间等)

我想创建这种类型的许多实例

vars()['Segment'+str(segment_number)] = SignalParam()

这条线工作,我可以创建变量“Segment1”,“Segment2”,....

我的问题是:我想把那些变种称为

"Segment"+segment_number.freqency=33

I created a class called SignalParam in python containing different properties (frequency, voltage, time, etc)

I would like to create many instances of this type

vars()['Segment'+str(segment_number)] = SignalParam()

this line is working and i can create variables "Segment1", "Segment2", ....

My question is: i would like to call those variabes like

"Segment"+segment_number.freqency=33

原文:https://stackoverflow.com/questions/30851272
2024-04-23 17:04

满意答案

与事务类似,语句也是原子的。 一旦语句开始执行数据更改,就会保存原始状态(实际上会记录更改)。 如果语句因任何原因失败(超时,与刚刚提交的事务冲突),则必须将更改恢复为原始状态。 该语句将报告失败但事务仍处于打开状态,您可以继续执行该事务,就像从未执行过该语句一样。

这实际上类似于保存点 - 您可以想象在记录每个语句保存点之前和语句完成之后,保存点已提交。 但是,这对外部事务或保存点没有影响。


Similarly to transaction, statement is atomic as well. Once the statement starts performing the data changes, the original state is saved (actually the changes are recorded). If statement fails for whatever reason (timeout, conflict with just committed transaction) the changes must be reverted to the original state. The statement will report a failure but the transaction is still open and you can continue with the transaction just like the statement was never executed.

This is actually similar to savepoint - you can imagine that before each statement savepoint is recorded and after statement is completed, the savepoint is committed. This has no influence on outer transaction or savepoints however.

相关问答

更多

MySQL中的语句回滚与事务回滚(Statement rollback vs transaction rollback in MySQL)

与事务类似,语句也是原子的。 一旦语句开始执行数据更改,就会保存原始状态(实际上会记录更改)。 如果语句因任何原因失败(超时,与刚刚提交的事务冲突),则必须将更改恢复为原始状态。 该语句将报告失败但事务仍处于打开状态,您可以继续执行该事务,就像从未执行过该语句一样。 这实际上类似于保存点 - 您可以想象在记录每个语句保存点之前和语句完成之后,保存点已提交。 但是,这对外部事务或保存点没有影响。 Similarly to transaction, statement is atomic as wel...

@Test之后的回滚事务(Rollback transaction after @Test)

只需在测试之上添加@Transactional注释: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"testContext.xml"}) @Transactional public class StudentSystemTest { 默认情况下,Spring将会围绕你的测试方法和@Before / @After回调开始一个新的事务,最后回滚。 默认情况下,在上下文中有一些事务管理器就足够...

使用带有mysql的Transaction时,我们可以进行多次回滚吗?(Can we have multiple rollback while using Transaction with mysql)

使用您的代码,将只执行一次回滚,具体取决于条件。 您可以编写多个DB::rollback() ,只要只执行一个。 要回答这个问题,可以进行多次回滚,如果使用嵌套事务,并使用回滚/提交返回上一级别。 With your code, only one rollback will be executed, depending on the conditions. You can write multiple DB::rollback(), as long as only one gets execute...

Mysql嵌套事务与回滚(Mysql nested transaction with rollback)

您将需要在两个过程中处理事务,但是调用另一个过程的proc应检查返回值并基于此回滚它的事务。 这是内部过程的一个例子: 如何检测MySQL存储过程中的回滚? 然后,您将检查p_return_code并执行父事务的回滚。 编辑: 我认为发生的是内部SP COMMIT或ROLLBACK影响外部SP TRANSACTION。 这段代码对我有用,如果内部SP失败,它会回滚两个插入语句。 首先调用ab()工作,插入新的用户记录并插入新的游戏记录,如果我们从游戏表中删除记录并再次运行ab(),因为用户ID已经...

NHibernate MySql事务不会回滚(NHibernate MySql transaction does not rollback)

如果您需要交易,请确保您使用的是InnoDb存储引擎,而不是MyIsam。 本文有一些很好的解释和建议。 Make sure you are using InnoDb storage engine and not MyIsam if you need transactions. This article has some good explanations and advises.

MySQL ROLLBACK实际上没有回滚(MySQL ROLLBACK not actually rolling back)

您的OPTIMIZE TABLE语句导致隐式提交。 我不确定你要做什么,但它看起来你可以缩短你的代码: $dbh->exec("OPTIMIZE TABLE `reservations`"); 所有其他代码只是使工作更复杂,没有任何好处。 我还假设您正在使用InnoDB表,因为MyISAM表无论如何都不支持事务。 MyISAM表上的每个DDL或DML操作都会立即隐式提交。 顺便说一下,缓冲查询与事务无关。 它们与一次获取一行SELECT结果集有关,而不是在PHP中将整个结果集提取到内存中,然后迭...

mysql事务(提交和回滚)(mysql transaction (commit and rollback))

要在同一事务中执行多个命令,请确保将事务对象分别分配给每个命令: Dim selectCmd As MySqlCommand = con.CreateCommand() Dim insertCmd As MySqlCommand = con.CreateCommand() selectCmd.CommandText = "SELECT ..." insertCmd.CommandText = "INSERT ..." Dim sqlTran As MySqlTransaction = con....

系统崩溃后mysql事务会回滚吗?(Will mysql transactions rollback after system crash?)

有不同类型的崩溃。 MySQL服务器可能崩溃(就像你杀了它)或整个操作系统可能崩溃(就像拔掉机器一样)。 您应该开始阅读的内容是关于二进制日志及其工作原理以及InnoDB引擎的恢复过程 There are different kind of crashes. The MySQL server can crash (like if you kill it) or the whole Operating system can crash (like if you unplug the machine)...

MySQL InnoDB事务回滚不起作用(MySQL InnoDB transaction rollback is not working)

经过一些调试,我发现了背后的原因。 基本上这个方法: Model::createOrUpdate($data); 正在调用存储过程,所以如果有人遇到此问题,请检查是否包含此问题。 After some debugging, I found the reason behind that. Basically the method: Model::createOrUpdate($data); is calling a stored procedure within, so if anyone is...

MySQL Workbench:启动事务似乎在回滚之前提交(MySQL Workbench: Start Transaction seems to be committed before rollback)

MySQL Workbench默认启用自动提交。 在SQL编辑器中有一个工具栏按钮,可用于随意切换自动提交: MySQL Workbench enables auto commit by default. In the SQL editor there is a toolbar button that can be used to toggle auto commit at will:

相关文章

更多

Python 字符串操作

Python 字符串操作,字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能 ...

Python字符串格式化

字符串的格式化 在python中也有类似于c中的printf()的格式输出标记。在python中格式化 ...

Java String类

Java String类 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提 ...

python2和python3的区别

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速 ...

redis 字符串(String) SET 操作

命令格式: SET key value 把字符串值value存储到key中。如果存在此key,SE ...

Python 列表(list)操作

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结 ...

遍历字符串每个字符,Stringreader是不是要比string.toCharArray来的快?

我想遍历一个字符串的每一个字符,以前是用string.toCharArray()来转换成一个char[ ...

探索 Python,第 1 部分: Python 的内置数值类型

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重 ...

Python学习笔记

好久没有写了,还不是近期刚过的期末考试和期中考试 最近因为一个微信公众平台大赛在学phthon 找了本 ...

Python的文件类型

Python的文件类型 Python有三种文件类型,分别是源代码文件、字节码文件和优化代码文件 源代 ...

最新问答

更多

sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)

否(它不会使它们无法访问),是(您可以在没有停机的情况下运行它)。 sp_updatestats可以在没有停机的情况下针对实时数据库运行。 No (it doesn't make them inaccessible), and Yes (you can run it without downtime). sp_updatestats can be run against a live database without downtime.

如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)

最终,我们选择了使用Spark Framework for Java实现的后端REST API。 这可能不是最强大的,用户反馈一直是个问题。 我们将命令行界面拆分为提交REST调用,并将结果显示给用户。 Ultimately, we chose to go the route of having a backend REST API that was implemented with the Spark Framework for Java. This may not be the most r

AESGCM解密失败的MAC(AESGCM decryption failing with MAC)

您不能将Encoding.UTF8.GetString应用于任意二进制数据。 它只能解码使用UTF-8编码字符串的结果的字节。 .net实现将默默地破坏数据,而不是默认情况下抛出异常。 您应该使用Base64: Convert.FromBase64String和Convert.ToBase64String You can't apply Encoding.UTF8.GetString to arbitrary binary data. It can only decode bytes that

Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)

我希望能看到更多你的Sass代码等,但我的猜测是你需要在所有嵌套行上使用nest行为。 在我看来,基金会在Sass中的行主要是为了在一个层面上使用。 嵌套在另一行中的任何行都应使用nest行为,除非您希望在列上添加额外的填充。 在你的CodePen中,我能够通过向所有行添加一类collapse来修复列上填充的问题,我认为这与执行$behavior: nest相同$behavior: nest在Sass中$behavior: nest :

湖北京山哪里有修平板计算机的

京山有个联想的专卖店,那里卖平板电脑,地址在中百前面的十字路口右拐 ,他们应该会提供相关的维修服务。

SimplePie问题(SimplePie Problem)

我怀疑由于内容的性质(包含代码),stackoverflow提要不起作用。 我使用许多feed解析器看似“正常”的feed有类似的问题,尽管我最近运气最多的是Zend_Feed。 试试吧 I suspect the stackoverflow feed is not working due to the nature of the content (contains code). I have had similar issues with seemingly "normal" feeds us

在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)

是的,您可以通过getApplicationContext()任意数量的时间(后台任务), getApplicationContext()仅返回应用程序的context 。 Yes, you can pass getApplicationContext() any number of time (Background Tasks ) you want, getApplicationContext() simply returns context of the application.

HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)

这是我最终做的事情: 我无法以这种方式提供完全访问权限,而是在project level folder中设置了一个虚拟HTML页面,该页面单击自身以重定向到位于separate, non-project util folder的HTML文件。 这允许我保留除了那个之外的所有内容,非常小的文件分开但不存在文件访问问题。 Here is what I ended up doing: I wasn't able to provide full access exactly this way, but

为什么我会收到链接错误?(Why do I get a linker error?)

看起来您的编译器错误地将名称引入到全局名称空间中,而不是C ++ 11 3.5 / 7中指定的最内层名称空间( Bushman ): 如果没有找到具有链接的实体的块范围声明来引用某个其他声明,那么该实体是最内层封闭名称空间的成员。 代码按照预期在GCC上编译: http : //ideone.com/PR4KVC 你应该能够通过在构造函数的块作用域中声明它之前(或代替它)在正确的名称空间中声明该函数来解决该bug。 但是,我无法访问您的编译器来测试它。 It looks like your co

如何正确定义析构函数(How to properly define destructor)

在C ++中,你需要手动释放内存。 没有垃圾收集器。 您显然需要在析构函数内手动释放内存。 如果您使用new分配内存,则需要对在deconstructor中使用new分配的每个资源使用delete ,例如: class1::~class1(void) { delete resource1; delete resource2; etc... } In C++ you need to free the memory manually. There's no garbage