Namespacing and classes

前端 未结 2 641
不思量自难忘°
不思量自难忘° 2021-01-02 19:10

I\'m trying to write some (in my opinion) readable code in Python. I need a module that will contain a number of classes. Theoretically I know everything that is needed to a

相关标签:
2条回答
  • 2021-01-02 19:26

    If you want to avoid hardcoding module paths, file, class, and function names in many places, you could do something like the following which dynamically imports (almost) all the Python files it finds in a package's subdirectory.

    The key is remembering that the __init__.py file is a largely unconstrained Python script itself, and is therefore free to perform fairly sophisticated processing of its own to determine what's in the package's namespace.

    File package\__init__.py:

    def _import_package_files():
        """ Dynamically import all the Python modules in this module's sub directory. """
        import os
        import sys
        import traceback
    
        package_path = os.path.split(__file__)[0]
        package_directory = os.path.split(package_path)[1]
    
        for fn in os.listdir(package_directory):
            globals_, locals_ = globals(), locals()
            # process all python files in directory that don't start with underscore
            if fn[0] != '_' and fn.split('.')[-1] in ('py', 'pyw'):
                modulename = fn.split('.')[0] # filename without extension
                subpackage = ".".join([package_directory, modulename])
                try:
                    module = __import__(subpackage, globals_, locals_, [modulename])
                except:
                    traceback.print_exc(file=sys.stdout)
                    raise # reraise exception
    
    _import_package_files()
    

    File package\Class1.py:

    class Class1(object):
        pass
    

    File package\Class2.py:

    class Class2(object):
        pass
    

    File package\Class3.py:

    class Class3(object):
        pass
    

    File usepackage.py:

    import package
    
    print(package.Class1)
    print(package.Class2)
    print(package.Class3)
    

    Output from running usepackage.py:

    <module 'package.Class1' from 'C:\Files\package\Class1.pyc'>
    <module 'package.Class2' from 'C:\Files\package\Class2.pyc'>
    <module 'package.Class3' from 'C:\Files\package\Class3.pyc'>
    
    0 讨论(0)
  • 2021-01-02 19:28

    Import them all within __init__.py, and then have the consumer import the package.

    from .module1 import Class1
    from .module2 import Class2
     ...
    
    0 讨论(0)
提交回复
热议问题