How do I call a member function from another file in python

Question:

I am trying to create a multi-file environment for my project. The File structure is as such

project_folder
   |- FileA.py
   |- FileB.py

The FileA.py has mainly the script in it and also I have improted FileB here
FileA.py

import os,sys
from FileB import temp_class as T

// lines of code 
str = "somestring"
A, B, C = T.func(str)

// lines of code

FileB.py

import os,sys

class temp_class:
    def func(self, S):
        //lines of code
        return (someA,someB,someC)

But I am getting the error as

TypeError: unbound method func() must be called with temp_class instance as first argument (got str instance instead)

Asked By: RC0993

||

Answers:

You just need to instantiate it: T_inst = T(), and then use T_inst.func().

To expand: the error message tells you that it expects to find an instance of T (temp_class) as its first argument. When you call a method of a class instance, that very instance is passed as the first argument (usually called "self"). In this case you tried to invoke it as a method of the class itself (like a static method) so this doesn’t happen, and self instead was assigned the string that you passed (with the second argument getting None instead of a string).

If you don’t want to instantiate the class then you could either declare func a static method (or in the docs here) of temp_class, or simply make temp_class a module instead of a class (i.e, define its "methods" outside of any class, with the hierarchical structure provided by the way you structure your modules; in your case, to obtain behavior identical to that which you seem to be going for: a directory called FileB that contains a __init__.py file (to make it function as a module) and a temp_class.py file with the function func defined within it).

Answered By: Shay

Since function func is in a class called temp_class, it has self as the first argument, which expects a class instance. You need to call it as T().func(str) to resolve this. Basically you need to instantiate the class using T() and use that instance to call func.

Answered By: compilation-error

the error message you are seeing , is telling you that you are trying to call "func" method of the "temp-class" without creating an instance of the class ,first in python instance methods need to be called on an instance of the class not in the class itself.
what i recommend is to edit the FileA.py:

import os, sys
from FileB import temp_class as T

inct_object = T()  # Create an instance of the temp_class
str = "loremloremlorem"
A, B, C = inct_object.func(str)
Answered By: ELMEHDI ACHAHED

In addition to all the good responses above, please take into account that the file which contains the function definitions and the file calling the functions must be in the same directory.

Answered By: jmvcollaborator
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.