GVKun编程网logo

生成器中的raise StopIteration和return语句有什么区别?

8

对于想了解生成器中的raiseStopIteration和return语句有什么区别?的读者,本文将提供新的信息,并且为您提供关于.net–Task<>和IAsyncOperation<>之间有什么区

对于想了解生成器中的raise StopIteration和return语句有什么区别?的读者,本文将提供新的信息,并且为您提供关于.net – Task <>和IAsyncOperation <>之间有什么区别?、@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别、c# – ConfigurationManager.GetSection和Configuration.GetSection有什么区别?、C# 中的 break 和 continue 语句有什么区别?的有价值信息。

本文目录一览:

生成器中的raise StopIteration和return语句有什么区别?

生成器中的raise StopIteration和return语句有什么区别?

我很好奇发生器中使用raise StopIterationreturn语句之间的区别。

例如,这两个功能之间有什么区别吗?

def my_generator0(n):    for i in range(n):        yield i        if i >= 5:            returndef my_generator1(n):    for i in range(n):        yield i        if i >= 5:            raise StopIteration

我猜想第二种方法是更“ pythonic”的方法(如果我错了,请纠正我),但据我所知,这两种方法都会引发StopIteration异常。

答案1

小编典典

无需显式引发,StopIteration因为这就是return生成器函数的裸露语句-
因此,它们是相同的。但是不,仅使用returnPython即可。

来自:http : //docs.python.org/2/reference/simple_stmts.html#the-return-
statement(对Python 3.2有效)

在生成器函数中,return语句不允许包含expression_list。在这种情况下,简单的返回指示生成器已完成,并将导致StopIteration升高。

或就像@Bakuriu指出的那样-生成器的语义对于Python 3.3略有变化,因此以下更合适:

在生成器函数中,return语句指示生成器已完成,并将引起StopIteration升高。返回的值(如果有)用作构造StopIteration的参数,并成为StopIteration.value属性。

.net – Task <>和IAsyncOperation <>之间有什么区别?

.net – Task <>和IAsyncOperation <>之间有什么区别?

我正在写一个地铁应用程序

这样做:

HttpClient client = new HttpClient();
    var bytes = await client.GetByteArrayAsync(new Uri("www.microsoft.com"));

这不是:

var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
    var file = await folder.GetFileAsync("text.txt");

第一个返回任务<>,第二个返回IAsyncoperation<>

有什么不同?为什么有两种不同的类型?如何修复第二个样本?

IAsyncoperation是一种城域异步操作.您可以等待IAsyncoperation.

但是,您不能将IAsyncoperation与Task.WhenAll或Task.WhenAny一起使用.要使用IAsyncoperation实例与这些方法,您应该调用StartAsTask扩展方法,因此:

var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
var fileTask = folder.GetFileAsync("text.txt").StartAsTask();

@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别

@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别

谁能解释一下:

@Resource
UserTransaction objUserTransaction;

EntityManager.getTransaction();

还有什么是容器管理的交易?如果要在事务中的表中插入三行,应该如何在会话外观中执行此操作。

c# – ConfigurationManager.GetSection和Configuration.GetSection有什么区别?

c# – ConfigurationManager.GetSection和Configuration.GetSection有什么区别?

我正在尝试基于AppSettings创建自定义配置文件部分:
<configSections>
  <section name="customConfiguration" 
           type="System.Configuration.AppSettingsSection,System.Configuration,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>

当我尝试通过ConfigurationManager.GetSection(“customConfiguration”)读取它时,返回的对象是System.Configuration.keyvalueInternalCollection类型.我无法读取此集合的值,虽然我可以看到键,但我无法将其转换为AppSettingsSection.

This Stackoverflow回答建议我应该使用

Configuration config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection customSettingSection = 
    (AppSettingsSection)config.GetSection("customConfiguration");

这很有效.我的问题是:ConfigurationManager.GetSection()和Configuration.GetSection()之间有什么区别?我何时应该使用另一个?何时应该使用另一个?

解决方法

根据配置类 http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx上的MSDN文档,

If your application needs read-only access to its own configuration,it is recommended that you use the 07001 method overloads for Web applications. For client application,use the 07002 method.

These methods provide access to the cached configuration values for the current application,which has better performance than the Configuration class.

具体来说,在客户端应用程序中,ConfigurationManager检索通过合并应用程序配置文件,本地用户配置文件和漫游配置文件获得的配置文件.

C# 中的 break 和 continue 语句有什么区别?

C# 中的 break 和 continue 语句有什么区别?

c# 中的 break 和 continue 语句有什么区别?

break 语句终止循环,并将执行转移到紧随循环后面的语句。

Continue 语句使循环跳过其主体的其余部分,并在重复之前立即重新测试其条件。

当循环内遇到break语句时,循环立即终止,程序控制在循环后的下一个语句处恢复。

C#中的continue语句的工作方式有点像break陈述。然而, continue 不是强制终止,而是强制进行循环的下一次迭代,并跳过其间的任何代码。

以下是在 while 循环中使用 continue 语句的完整代码 -

示例

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {

         /* local variable definition */
         int a = 10;

         /* loop execution */
         while (a > 20) {
            if (a == 15) {
               /* skip the iteration */
               a = a + 1;
               continue;
            }
            Console.WriteLine("value of a: {0}", a);
            a++;
         }
         Console.ReadLine();
      }
   }
}
登录后复制

下面是一个break语句的示例−

示例

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 10;

         /* while loop execution */
         while (a < 20) {
            Console.WriteLine("value of a: {0}", a);
            a++;

            if (a > 15) {
               /* terminate the loop using break statement */
               break;
            }
         }
         Console.ReadLine();
      }
   }
}
登录后复制

以上就是C# 中的 break 和 continue 语句有什么区别?的详细内容,更多请关注php中文网其它相关文章!

关于生成器中的raise StopIteration和return语句有什么区别?的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于.net – Task <>和IAsyncOperation <>之间有什么区别?、@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别、c# – ConfigurationManager.GetSection和Configuration.GetSection有什么区别?、C# 中的 break 和 continue 语句有什么区别?的相关知识,请在本站寻找。

本文标签: