i have the following code in a module called code_database.py
class Entry():
def enter_data(self):
self.title = input('enter a title: ')
print('enter the code, press ctrl-d to end: ')
self.code = sys.stdin.readlines()
self.tags = input('enter tags: ')
def save_data(self):
with open('entry.pickle2', 'ab') as f:
pickle.dump(self, f)
in idle the class-defined methods work fine:
>>> import code_database
>>> entry = code_database.Entry()
>>> entry.enter_data()
enter a title: a
enter the code, press ctrl-d to end:
benter tags: c
>>> entry.title
'a'
>>> entry.code
['b']
>>> entry.tags
'c'
>>>
however if i call the module from an external program and try to call the methods, they raise a NameError:
import code_database
entry = code_database.Entry()
entry.enter_data()
entry.save_data()
causes this in the terminal:
$python testclass.py
enter a title: mo
Traceback (most recent call last):
File "testclass.py", line 6, in <module>
entry.enter_data()
File "/home/mo/python/projects/code_database/code_database.py", line 8, in enter_data
self.title = input('enter a title: ')
File "<string>", line 1, in <module>
NameError: name 'mo' is not defined