Why am I getting Name Error when importing a class?

前端 未结 4 1044
我在风中等你
我在风中等你 2020-12-18 08:07

I am just starting to learn Python, but I have already run into some errors. I have made a file called pythontest.py with the following contents:



        
4条回答
  •  一个人的身影
    2020-12-18 08:42

    You need to do:

    >>> import pythontest
    >>> f = pythontest.Fridge()
    

    Bonus: your code would be better written like this:

    def __init__(self, items=None):
        """Optionally pass in an initial dictionary of items"""
        if items is None:
             items = {}
        if not isinstance(items, dict):
            raise TypeError("Fridge requires a dictionary but was given %s" % type(items))
        self.items = items
    

提交回复
热议问题