Import selected functions and classes in python

Viewed 40

How should I import selected functions and classes from script using only 'import script'?

#script.py
def func_a():
    return 1

def func_b():
    return 1

def func_c():
    return 1

def func_d():
    return 1


class ClsA(object):
    def func(self):
        return 10
    

class ClsB(object):
    def func(self):
        return 30

When I use 'import script' in another script or Jupyter notebook, I'd like to import only functions func_a and func_b, class ClsA. Is it possible to do that?

Thank you in advance!

2 Answers

You can import all functions and classes like this

from script import *

To import specific classes and functions

from script import func_a, ClsA, func_b
Related