相关疑难解决方法(0)

"yield"关键字有什么作用?

yieldPython中关键字的用途是什么?它有什么作用?

例如,我试图理解这段代码1:

def _get_child_candidates(self, distance, min_dist, max_dist):
    if self._leftchild and distance - max_dist < self._median:
        yield self._leftchild
    if self._rightchild and distance + max_dist >= self._median:
        yield self._rightchild  
Run Code Online (Sandbox Code Playgroud)

这是来电者:

result, candidates = [], [self]
while candidates:
    node = candidates.pop()
    distance = node._get_dist(obj)
    if distance <= max_dist and distance >= min_dist:
        result.extend(node._values)
    candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
return result
Run Code Online (Sandbox Code Playgroud)

_get_child_candidates调用该方法时会发生什么?列表是否返回?单个元素?它又被召唤了吗?后续通话何时停止?


1.代码来自Jochen Schulz(jrschulz),他为度量空间创建了一个很棒的Python库.这是完整源代码的链接:模块mspace.

python iterator yield generator coroutine

9664
推荐指数
46
解决办法
212万
查看次数

如何制作一系列功能装饰器?

如何在Python中创建两个装饰器来执行以下操作?

@makebold
@makeitalic
def say():
   return "Hello"
Run Code Online (Sandbox Code Playgroud)

...应该返回:

"<b><i>Hello</i></b>"
Run Code Online (Sandbox Code Playgroud)

我不是试图HTML在一个真实的应用程序中这样做 - 只是试图了解装饰器和装饰器链是如何工作的.

python decorator python-decorators

2647
推荐指数
17
解决办法
49万
查看次数

Python:重载构造函数的问题

警告:我一直在学习Python 10分钟,所以对任何愚蠢的问题表示歉意!

我写了以下代码,但是我得到以下异常:

消息文件名行位置跟踪节点31 exceptions.TypeError:此构造函数不带参数

class Computer:

    name = "Computer1"
    ip = "0.0.0.0"
    screenSize = 17


    def Computer(compName, compIp, compScreenSize):
        name = compName
        ip = compIp
        screenSize = compScreenSize

        printStats()

        return

    def Computer():
        printStats()

        return

    def printStats():
        print "Computer Statistics: --------------------------------"
        print "Name:" + name
        print "IP:" + ip
        print "ScreenSize:" , screenSize // cannot concatenate 'str' and 'tuple' objects
        print "-----------------------------------------------------"
        return

comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

python exception constructor-overloading

7
推荐指数
2
解决办法
5226
查看次数