python - Why am I getting Name Error when importing a class? -


i starting learn python, have run errors. have made file called pythontest.py following contents:

class fridge:     """this class implements fridge ingredients can added , removed individually        or in groups"""     def __init__(self, items={}):         """optionally pass in initial dictionary of items"""         if type(items) != type({}):             raise typeerror("fridge requires dictionary given %s" % type(items))         self.items = items         return 

i want create new instance of class in interactive terminal, run following commands in terminal: python3

>> import pythontest >> f = fridge() 

i error:

traceback (most recent call last):   file "<stdin>", line 1, in <module> nameerror: name 'fridge' not defined 

the interactive console cannot find class made. import worked successfully, though. there no errors.

you need do:

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

bonus: code better written this:

def __init__(self, items=none):     """optionally pass in initial dictionary of items"""     if items none:          items = {}     if not isinstance(items, dict):         raise typeerror("fridge requires dictionary given %s" % type(items))     self.items = items 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -