如何在 Python 中定义海龟而不创建新海龟?
How do I define a turtle in Python without creating a new one?
我正在编写一个游戏并使用 turtles 导入图像,因为这是我知道的唯一方法,但我遇到了一个问题,当我尝试在其原始功能之外定义我的 turtle(以便我可以在其他地方使用它),它会创建另一个具有相同名称的海龟,而不是让第一只海龟实际执行 'goto' 行。这是我的代码中出现的问题的示例。 (这不是我的实际代码,但是,问题是一样的,不受我其他代码的影响:)
import turtle
def example():
a = turtle.Turtle()
example()
a.goto(100,0)
这给出 NameError: name 'a' is not defined
。然后当我尝试定义 a
时,像这样:
example()
a = turtle.Turtle()
a.goto(100,0)
输出是两只乌龟,其中只有一只执行 goto()
命令。
感谢您花时间阅读我的 post,我是编码新手,这一直困扰着我!
发生这种情况是因为函数内的变量绑定到函数范围。基本上,如果你在函数 example()
中定义变量 a
,该变量只存在于该函数中,并且会在函数调用结束时消亡。
如果您尝试 运行 以下内容:
def foo():
bar = 2
foo()
print bar
输出
> NameError: name 'bar' is not defined
发生这种情况是因为 bar
只住在 foo
里面。但是,您可以使用 global
语句将 bar
绑定到全局范围。
def foo():
global bar
bar = 2
foo()
print bar
输出:
> 2
但这不是一个很好的做法。相反,在您的情况下,您可以在调用 example
时 return 对象,从而获得您创建的对象。
import turtle
def example():
a = turtle.Turtle()
return a
a = example()
a.goto(100,0)
我认为你非常接近。我要做的一件事是确保您的 python 函数 example()
returns 是一个对象。现在它 returns 什么都没有。此外,请务必将函数 returns 分配给变量。我会尝试:
import turtle
def example():
a_turtle = turtle.Turtle()
return a_turtle
a = example()
a.goto(100,0)
b = example()
b.goto(50,0)
我不太了解海龟包,所以我不确定是否有办法让多只海龟用一个命令响应,但上面的代码对我来说很有效,可以得到两只海龟,a
和 b
移动。
编辑:另外,就像其他答案所说的那样,阅读一些关于名称空间和函数的内容。这将有助于弄清楚什么地方可以访问。
我阅读和编写了大量 Python 海龟代码,我看到处理这种情况的最常见方法是:
import turtle
def example():
a.dot() # do something with the turtle
a = turtle.Turtle()
example()
a.goto(100, 0)
或者,同样经常:
import turtle
def example(t):
t.dot() # do something with the turtle
a = turtle.Turtle()
example(a)
a.goto(100, 0)
这些方法也反映出 turtles 总是表现得像 global 实体(它们在 turtle 库中的列表中注册,因此永远不会被垃圾收集。)在函数中创建一个,并且不返回它,不会使它 local 成为函数——只是指向它的指针是本地的。乌龟继续存在 nearly 之后无法访问。
我正在编写一个游戏并使用 turtles 导入图像,因为这是我知道的唯一方法,但我遇到了一个问题,当我尝试在其原始功能之外定义我的 turtle(以便我可以在其他地方使用它),它会创建另一个具有相同名称的海龟,而不是让第一只海龟实际执行 'goto' 行。这是我的代码中出现的问题的示例。 (这不是我的实际代码,但是,问题是一样的,不受我其他代码的影响:)
import turtle
def example():
a = turtle.Turtle()
example()
a.goto(100,0)
这给出 NameError: name 'a' is not defined
。然后当我尝试定义 a
时,像这样:
example()
a = turtle.Turtle()
a.goto(100,0)
输出是两只乌龟,其中只有一只执行 goto()
命令。
感谢您花时间阅读我的 post,我是编码新手,这一直困扰着我!
发生这种情况是因为函数内的变量绑定到函数范围。基本上,如果你在函数 example()
中定义变量 a
,该变量只存在于该函数中,并且会在函数调用结束时消亡。
如果您尝试 运行 以下内容:
def foo():
bar = 2
foo()
print bar
输出
> NameError: name 'bar' is not defined
发生这种情况是因为 bar
只住在 foo
里面。但是,您可以使用 global
语句将 bar
绑定到全局范围。
def foo():
global bar
bar = 2
foo()
print bar
输出:
> 2
但这不是一个很好的做法。相反,在您的情况下,您可以在调用 example
时 return 对象,从而获得您创建的对象。
import turtle
def example():
a = turtle.Turtle()
return a
a = example()
a.goto(100,0)
我认为你非常接近。我要做的一件事是确保您的 python 函数 example()
returns 是一个对象。现在它 returns 什么都没有。此外,请务必将函数 returns 分配给变量。我会尝试:
import turtle
def example():
a_turtle = turtle.Turtle()
return a_turtle
a = example()
a.goto(100,0)
b = example()
b.goto(50,0)
我不太了解海龟包,所以我不确定是否有办法让多只海龟用一个命令响应,但上面的代码对我来说很有效,可以得到两只海龟,a
和 b
移动。
编辑:另外,就像其他答案所说的那样,阅读一些关于名称空间和函数的内容。这将有助于弄清楚什么地方可以访问。
我阅读和编写了大量 Python 海龟代码,我看到处理这种情况的最常见方法是:
import turtle
def example():
a.dot() # do something with the turtle
a = turtle.Turtle()
example()
a.goto(100, 0)
或者,同样经常:
import turtle
def example(t):
t.dot() # do something with the turtle
a = turtle.Turtle()
example(a)
a.goto(100, 0)
这些方法也反映出 turtles 总是表现得像 global 实体(它们在 turtle 库中的列表中注册,因此永远不会被垃圾收集。)在函数中创建一个,并且不返回它,不会使它 local 成为函数——只是指向它的指针是本地的。乌龟继续存在 nearly 之后无法访问。