gpt4 book ai didi

python - 在函数中访问函数(嵌套函数?)

转载 作者:太空狗 更新时间:2023-10-29 19:31:41 25 4
gpt4 key购买 nike

<分区>

这里是 Python 菜鸟。如何在“fib”函数中获取“inner”函数?

from time import sleep

class Fibonacci(object):

def __init__(self, a, b, limit=50):
self.a = a
self.b = b
self.limit = limit

def fib(self):

while self.a < self.limit:
c = self.a + self.b
sleep(1)
print self.a,
self.b = self.a
self.a = c

def inner(self):
print 'Damn it! Just print already!'


j = Fibonacci(0,1,2)
j.fib()

## This doesn't work. Gives an "AttibuteError: 'function' object has no attribute 'inner'"
j.fib.inner()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com