小编Pyr*_*onk的帖子

分配(收益)到一个变量

首先,我想提一下,我对Python并不是特别熟悉。最近,我不得不让自己熟悉一个让我大吃一惊的代码示例,而我却无法对其进行“翻译”。我看过的各种文档和文章也没有帮助:

这是相关功能的简化版本:

@coroutine
def processMessage(receiver):
    global userID
    #...
    while True:
        msg = (yield)
        try:
            #...
        except Exception as err:
            #...
Run Code Online (Sandbox Code Playgroud)

我无法理解它的作用,因此无法“遍历”代码。我的问题是“此功能有什么作用?” 和“此功能遵循什么顺序?”

使我失望的线是msg = (yield)。我不知道它要达到什么目的。直觉告诉我,它只是在收到新消息时抓住它们,但我不明白为什么。如果有人知道并且我提供了足够的信息,我将不胜感激。

Try条款:

if msg['event'] == 'message' and 'text' in msg and msg['peer'] is not None:
    if msg['sender']['username'] == username:
        userID = msg['receiver']['peer_id']
        config.read(fullpath + '/cfg/' + str(userID) + '.cfg')
        if config.has_section(str(userID)):
            log('Config found')
            readConfig()
            log('Config loaded')
        else:
            log('Config not found')
            writeConfig()
            log('New config created') …
Run Code Online (Sandbox Code Playgroud)

python yield

4
推荐指数
1
解决办法
566
查看次数

如何在动态附加的元素上附加点击事件?

我已经使用HTML和CSS发出了一些警报,我想通过jQuery添加和删除这些警报,但是问题是当按下关闭按钮时,我无法关闭动态添加的警报。

我也搜索了google和stack-overflow,但是我还不了解他们的工作方式,如果您能给我一些解释的答案,我将不胜感激。

$(document).ready(function() {

    // remove alert
    $(".close").on("click", function() {
        $(this)
            .parent(".alert")
            .slideUp(250)
            .promise()
            .done(function() {
                $(this).remove();
            });
    });

    // append alert
    const btn = $("#btn");
    btn.on("click", function() {
        $($(".alert")[$(".alert").length - 1])
            .after(
                '<div class="alert -ugly"> <header> Dynamically added</header> <div class="close"><img src="https://res.cloudinary.com/ajayrawat/image/upload/v1514205869/close_df3rub.svg" /> </div></div>'
            )
            .hide()
            .slideDown(250);
    });
});
Run Code Online (Sandbox Code Playgroud)
body {
  background-color: #ddd;
  font-family: sans-serif;
  font-size: 14px;
  letter-spacing: 1px;
}

.alert {
  display: block;
  position: relative;
  width: 100%;
  max-width: 30rem;
  padding: .5rem .8rem;
  margin: 1rem auto 0;
  border-radius: .2rem;
  color: …
Run Code Online (Sandbox Code Playgroud)

html jquery

3
推荐指数
1
解决办法
552
查看次数

标签 统计

html ×1

jquery ×1

python ×1

yield ×1