0

I'm trying to connect to a sqllite database using:

class Sqllite_utilities(object):

    def __init__(self):

        parser = argparse.ArgumentParser()
        parser.add_argument("-s","--source", type=str,
                            help="source table from db")
        args = parser.parse_args()
        print(args)


        source_table= args.source

        db_path = "sqlite:///"+settings.SETTINGS_PATH+"\\data.db"

        dataset_db = dataset.connect(db_path)
        self.dataset_table = dataset_db[source_table]


    def missing_ids(self):

        for user in self.dataset_table:
            print(user['id'])


if __name__ == '__main__':
    Sqllite_utilities.missing_ids(sys.argv[1])

When I do:

$ python find_missing_records.py -s test

I get:

Traceback (most recent call last):
  File "find_missing_records.py", line 41, in <module>
    Sqllite_utilities.missing_ids(sys.argv[1])
TypeError: unbound method missing_ids() must be called with Sqllite_utilities instance as first argument (got str instance instead)
(contact2E)

What am I doing wrong?

2 Answers 2

4

When doing:

Sqllite_utilities.missing_ids(sys.argv[1])

you're calling the missing_ids instance method with sys.argv[1] (a string) as self (functional way to call instance methods, but with the wrong object), which explains the error message.

It's clear that you have to create an instance of your object before being able to use the instance methods (the ones without @staticmethod or @classmethod decorators):

if __name__ == '__main__':
    s = Sqllite_utilities()

the constructor parses the arguments, using argparse.ArgumentParser which uses sys.argv by default, so leave that aside, it'll work.

Then call your method on the instance:

s.missing_ids()

s is implicitly passed as self when calling missing_ids

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you that works. With regard to "It's clear that you have to create an instance of your object before being able to use the instance methods (the ones without staticmethod or classmethod decorators)", are you saying that class and static methods do not need the class to be instantiated first, but other methods do?
yes, you can use class/static methods without any instance.
Thank you, that helps my understanding.
1

As suggested from the top answer to this question: unbound method f() must be called with fibo_ instance as first argument (got classobj instance instead)

Add to your code:

if __name__ == '__main__':
    obj = Sqllite_utilities()
    s.missing_ids()

2 Comments

Thank you for the reference
Happy to help mate :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.