对于以下代码:

logger.debug('message: {}'.format('test'))

Pylint产生以下警告:

logging-format-interpolation (W1202): 在日志函数中使用%格式,并将%参数传递为 当日志语句的调用形式为 “日志记录。(format_string.format (format_args…)”。这样的 调用应该使用%格式,而将插值留给 日志函数,通过将参数作为参数传递。

我知道我可以关闭这个警告,但我想了解它。我假设使用format()是Python 3中输出语句的首选方式。为什么记录器语句不是这样?

我想从表中移除约束条件。我的问题是:

ALTER TABLE `tbl_magazine_issue` 
DROP CONSTRAINT `FK_tbl_magazine_issue_mst_users`

但是我得到了一个错误:

#1064 -你的SQL语法错误;请查看MySQL服务器版本对应的手册,在第一行使用“constraint FK_tbl_magazine_issue_mst_users”附近的正确语法

我是一名新的Python程序员,正在从2.6.4跳到3.1.1。一切都很好,直到我尝试使用'else if'语句。解释器在“else if”中的“if”后面给了我一个语法错误,原因我似乎不知道。

def function(a):
    if a == '1':
        print ('1a')
    else if a == '2'
        print ('2a')
    else print ('3a')

function(input('input:'))

我可能忽略了一些非常简单的东西;然而,我自己还没能找到答案。

这个问题是由我的另一个问题:如何在cdef等待?

网络上有大量关于asyncio的文章和博客文章,但它们都非常肤浅。我找不到任何关于asyncio实际是如何实现的,以及什么使I/O异步的信息。我试图阅读源代码,但它有数千行不是最高级的C代码,其中很多处理辅助对象,但最重要的是,它很难将Python语法和它将转换成的C代码联系起来。

Asycnio自己的文档就更没有帮助了。这里没有关于它如何工作的信息,只有一些关于如何使用它的指南,这些指南有时也会误导/写得很糟糕。

我熟悉Go的协程实现,并希望Python也能做同样的事情。如果是这样的话,我在上面链接的帖子中出现的代码应该是有效的。既然它没有,我现在正试图找出原因。到目前为止,我最好的猜测如下,请纠正我的错误:

Procedure definitions of the form async def foo(): ... are actually interpreted as methods of a class inheriting coroutine. Perhaps, async def is actually split into multiple methods by await statements, where the object, on which these methods are called is able to keep track of the progress it made through the execution so far. If the above is true, then, essentially, execution of a coroutine boils down to calling methods of coroutine object by some global manager (loop?). The global manager is somehow (how?) aware of when I/O operations are performed by Python (only?) code and is able to choose one of the pending coroutine methods to execute after the current executing method relinquished control (hit on the await statement).

换句话说,这是我试图将一些asyncio语法“糖化”成更容易理解的东西:

async def coro(name):
    print('before', name)
    await asyncio.sleep()
    print('after', name)

asyncio.gather(coro('first'), coro('second'))

# translated from async def coro(name)
class Coro(coroutine):
    def before(self, name):
        print('before', name)

    def after(self, name):
        print('after', name)

    def __init__(self, name):
        self.name = name
        self.parts = self.before, self.after
        self.pos = 0

    def __call__():
        self.parts[self.pos](self.name)
        self.pos += 1

    def done(self):
        return self.pos == len(self.parts)


# translated from asyncio.gather()
class AsyncIOManager:

    def gather(*coros):
        while not every(c.done() for c in coros):
            coro = random.choice(coros)
            coro()

Should my guess prove correct: then I have a problem. How does I/O actually happen in this scenario? In a separate thread? Is the whole interpreter suspended and I/O happens outside the interpreter? What exactly is meant by I/O? If my python procedure called C open() procedure, and it in turn sent interrupt to kernel, relinquishing control to it, how does Python interpreter know about this and is able to continue running some other code, while kernel code does the actual I/O and until it wakes up the Python procedure which sent the interrupt originally? How can Python interpreter in principle, be aware of this happening?

大多数人说永远不要从析构函数抛出异常——这样做会导致未定义的行为。Stroustrup指出,“vector析构函数显式地为每个元素调用析构函数。这意味着如果元素析构函数抛出,则vector销毁失败…实际上没有很好的方法来防止析构函数抛出异常,因此标准库不保证元素析构函数是否抛出”(摘自附录E3.2)。

这篇文章似乎不是这么说的——抛出析构函数或多或少是可以的。

所以我的问题是,如果从析构函数抛出导致未定义的行为,你如何处理析构函数期间发生的错误?

如果在清理操作期间发生错误,您会忽略它吗?如果它是一个可以在堆栈中处理但不能在析构函数中处理的错误,那么从析构函数抛出异常难道没有意义吗?

显然,这种错误很少见,但也有可能发生。

在我的angular 4应用中,我有一个字符串

comment: string;
comment = "<p><em><strong>abc</strong></em></p>";

当我在html中提供这个文本时,比如

{{comment}}

然后显示:

<p><em><strong>abc</strong></em></p>

但是我需要以粗体和斜体的形式显示文本“abc”,比如 美国广播公司

我该怎么做呢?

最近我开始使用Python3,它缺乏xrange的伤害。

简单的例子:

Python2: 从时间导入时间为t def count (): St = t() [x for x in xrange(10000000)如果x%4 == 0] Et = t() 打印et-st count () Python3: 从时间导入时间为t def xrange (x): 返回iter(范围(x)) def count (): St = t() [x for x in xrange(10000000)如果x%4 == 0] Et = t() 打印(et-st) count ()

结果分别为:

1.53888392448 3.215819835662842

为什么呢?我是说,为什么xrange被删除了?这是一个很好的学习工具。对于初学者来说,就像我一样,就像我们所有人一样。为什么要移除它?谁能告诉我正确的PEP,我找不到。

我正在尝试理解如何使用可选类型提示。从PEP-484,我知道我可以使用可选的def测试(a: int = None)作为def测试(a:联盟[int, None])或def测试(a:可选[int])。

但是下面的例子呢?

def test(a : dict = None):
    #print(a) ==> {'a': 1234}
    #or
    #print(a) ==> None

def test(a : list = None):
    #print(a) ==> [1,2,3,4, 'a', 'b']
    #or
    #print(a) ==> None

如果Optional[type]似乎意味着与Union[type, None]相同的事情,为什么我应该使用Optional[]呢?

我有一个与这个问题非常相似的问题,但我仍然落后一步。我的Windows 7(抱歉)64位系统上只安装了一个版本的Python 3。

我按照这个链接安装了NumPy -正如问题中所建议的那样。安装很顺利,但当我执行

import numpy

我得到了以下错误:

导入错误:没有名为numpy的模块

我阅读了与此错误有关的其他线程,似乎我的问题与我迄今为止阅读的所有帖子有一个有趣的明显区别,即,迄今为止所有其他帖子都有关于用户创建的类或内置系统资源的错误。我在调用函数时遇到了这个问题,我不知道它是用来做什么的。什么好主意吗?

BOX_LENGTH = 100
turtle.speed(0)
fill = 0
for i in range(8):
    fill += 1
    if fill % 2 == 0:
        Horizontol_drawbox(BOX_LENGTH, fillBox = False)
    else:
        Horizontol_drawbox(BOX_LENGTH, fillBox = True)

    for i in range(8):
        fill += 1
        if fill % 2 == 0:
            Vertical_drawbox(BOX_LENGTH,fillBox = False)
        else:
            Vertical_drawbox(BOX_LENGTH,fillBox = True)

错误信息:

    Horizontol_drawbox(BOX_LENGTH, fillBox = True)
TypeError: Horizontol_drawbox() got multiple values for argument 'fillBox'