简体   繁体   English

在 python 中导入选定的函数和类

[英]Import selected functions and classes in python

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.当我在另一个脚本或 Jupyter 笔记本中使用“导入脚本”时,我只想导入函数 func_a 和 func_b,class ClsA。 Is it possible to do that?有可能这样做吗?

Thank you in advance!先感谢您!

from script import func_a, ClsA

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM