如何避免为许多函数重复某些参数? (Python)
How to avoid repeating certain arguments for many functions? (Python)
我正在使用 Evernote API 编写此应用程序,发现自己用相同的几个参数重复调用许多函数。有什么方法可以在不使用全局变量的情况下避免这种情况?
def get_all_notes(dev_token, noteStore):
def find_notes(notebook, dev_token, noteStore):
def main():
dev_token = ...
noteStote = ...
notes = get_all_notes(dev_token, noteStore)
notes_from_notebook1 = find_notes(notebooks[0], dev_token, noteStore)
如果您一遍又一遍地使用相同的参数,并且它们没有改变。也许需要让他们成为 class?
class MyNotesController:
def __init__(self, dev_token, noteStore):
self.dev_token = dev_token
self.noteStore = noteStore
def get_all_notes(self):
# Use self.dev_token and self.noteStore
def find_notes(self, notebook):
# Use self.dev_token and self.noteStore
def main():
dev_token = ...
noteStote = ...
my_ctrl = MyNotesController(dev_token, noteStote)
notes = my_ctrl.get_all_notes()
notes_from_notebook1 = my_ctrl.find_notes(notebooks[0])
我正在使用 Evernote API 编写此应用程序,发现自己用相同的几个参数重复调用许多函数。有什么方法可以在不使用全局变量的情况下避免这种情况?
def get_all_notes(dev_token, noteStore):
def find_notes(notebook, dev_token, noteStore):
def main():
dev_token = ...
noteStote = ...
notes = get_all_notes(dev_token, noteStore)
notes_from_notebook1 = find_notes(notebooks[0], dev_token, noteStore)
如果您一遍又一遍地使用相同的参数,并且它们没有改变。也许需要让他们成为 class?
class MyNotesController:
def __init__(self, dev_token, noteStore):
self.dev_token = dev_token
self.noteStore = noteStore
def get_all_notes(self):
# Use self.dev_token and self.noteStore
def find_notes(self, notebook):
# Use self.dev_token and self.noteStore
def main():
dev_token = ...
noteStote = ...
my_ctrl = MyNotesController(dev_token, noteStote)
notes = my_ctrl.get_all_notes()
notes_from_notebook1 = my_ctrl.find_notes(notebooks[0])