1

How do I call a function defined in a class in Python?

       import pypyodbc as pyodbc

        Master_Conn = 'Driver={SQL Server};Server=server\23;Database=DBname;UID=username;PWD=password;'
        Master_db = pyodbc.connect(Master_Conn)
        Master_Cursor = Master_db.cursor()

        class Test:

            def __init__(self):
                self.master_status = ""


            def Getmodel(self):

                self.master_status= dict(Master_Cursor.execute("select col1,col2 from tablename ").fetchall())
                print (self.master_status)
Test.Getmodel()

With above code, I get

TypeError: Getmodel() missing 1 required positional argument: 'self'

So I tried Test.Getmodel(self) and it resulted in

NameError: name 'self' is not defined.

I even tried both scenarios with if __name__== '__main__': but got same errors.

Marco
  • 2,007
  • 17
  • 28
Sel_Python
  • 191
  • 2
  • 7
  • 16
  • 1
    .... is there some reason you are creating a class you aren't planning to instantiate? Use `test_object = Test() # instantiation` then you can call your *method* using `test_object.Getmodel()` But think about it: your function definition is `def Getmodel(self):`, it requires a `self`, why did you expect `Test.Getmodel()` to work without the argument? – juanpa.arrivillaga Feb 16 '18 at 18:18
  • You gotta instantiate that thing broseph – SuperStew Feb 16 '18 at 18:25
  • 1
    " I even tried both scenarios with if name== 'main':" that seems that you also don't understand what that is used for. As mentioned, you need to instantiate your class, but you might also want to read [this](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – roganjosh Feb 16 '18 at 18:28
  • Possible duplicate of [Python: Call method by instance object: "missing 1 required positional argument: 'self'"](https://stackoverflow.com/questions/20023986/python-call-method-by-instance-object-missing-1-required-positional-argument) – Davis Herring Feb 16 '18 at 23:43

2 Answers2

2

You are defining Getmodel as an instance method. So it have to be called on an instance of the class Test.

To create an instance of class Test you can write

instance_of_test = Test()

Now you can call Getmodel on instance_of_test

instance_of_test.Getmodel()

You can shorten this process by writing

Test().Getmodel()

Note that self is (usually) passed hiddenly when calling an instance method; it represents the instance calling the method.


Consider the following class Pizza

class Pizza:

    def __init__(self, size):
        self.size = size

    def get_size(self):
        return self.size

First you need to create an instance of Pizza

mypizza = Pizza(42)

then you can call the instance method get_size on mypizza

mypizza.get_size()
Marco
  • 2,007
  • 17
  • 28
1

What your doing will work if you instantiate an object of the class Test first. Instantiation just means you call the class as if it were a method which. The following should work:

Test().Getmodel()

The () is what makes the instantiation happen so now the self essentially gets passed to the Getmodel() method because an object of the class Test now exists.

rigby
  • 223
  • 2
  • 10