2

I am not too far into python yet and here is the case.

Say I have one python file called functions.py which holds a class with my functions. Below is an example.

import json

class Functionalities:
    def addelement(element):
        # code goes here`

And I have another python file, kind of 'executable' script which does all the job using functions from functions.py class

Adding from . import functions doesn't help.

How to I call functions from the class from another file?

2

1 Answer 1

6

Unlike java, you don't have to use classes in python. If a class is used only as a holder for functions, chances are that you want a free function (one without a class) instead.

But to answer your actual question, you can use the import statement. That will run the other .py file, and make the namespace available so you can call things defined there.

functions.py:

import json

class Functionalities:
    def addelement(element):
        # code goes here`

main.py:

import functions
f = functions.Functionalities()
f.addelement('some element')
Sign up to request clarification or add additional context in comments.

2 Comments

I just thought i'd better sort functions into different classes (or we can say categories in my case) so not to make a mess in future when there will be a lot of functions in one file. But thanks for the answer and suggestion. It worked!
@MherHarutyunyan the pythonic way to sort functions in python is to use modules... not classes.

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.