cappuccino
cappuccino

Reputation: 43

How to avoid repeating certain arguments for many functions? (Python)

I am writing this app using Evernote API, and find myself calling many functions with the same few arguments repeatedly. Any way I can avoid this without using global variables?

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)

Upvotes: 1

Views: 2606

Answers (1)

André C. Andersen
André C. Andersen

Reputation: 9375

If you are using the same arguments over and over, and they don't change. Maybe it calls for making them a 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])

Upvotes: 13

Related Questions