<p>我刚开始学习Python,但我已经遇到了一些错误。我制作了一个名为<code>pythontest.py</code>的文件,其内容如下:</p>
<pre><code>class Fridge:
"""This class implements a fridge where ingredients can be added and removed individually
or in groups"""
def __init__(self, items={}):
"""Optionally pass in an initial dictionary of items"""
if type(items) != type({}):
raise TypeError("Fridge requires a dictionary but was given %s" % type(items))
self.items = items
return
</code></pre>
<p>我想在交互式终端中创建一个类的新实例,所以我在终端中运行以下命令:
Python3</p>
<pre><code>>> import pythontest
>> f = Fridge()
</code></pre>
<p>我得到这个错误:</p>
<pre><code>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Fridge' is not defined
</code></pre>
<p>交互控制台找不到我创建的类。不过,导入成功。没有错误。</p>