导入模块并通过 1 个对象传递所有方法和属性

问题描述 投票:0回答:1

嘿伙计们,

我对我的应用程序的实现有疑问。也就是说,我想实现在

importlib( )
的帮助下加载模块,并且可以在一个对象中输出所有方法和属性。我有以下内容 文件“sample.py”,需要导入:

from Modules import Module_Loader
   
class myapp :

    def __init__( Self , *Args , **KWT_Args ) :
        
        print( f"[  DEBUG  ] Initializing Class '{__class__.__name__}'" )   
        Self.Test = "TESTER"
        
    def Calculate( Self ) :
        print( "Calculating" )

class Testumgebung :

    Nothing = "NOTHING"

    def __init__( Self , *Args , **KWT_Args ) :
        
        print( f"[  DEBUG  ] Initializing Class 'Testumgebung'" )
        Self.OtherVar = "Anderes"
        
    def Neu( Self ) :
    
        Self.Probe = "NEUES"

现在我想在我的应用程序中加载此模块并使用“myapp( )”这两种方法 & 'TestEnvironment( )' 以及属性

Self.Test
Nothing
&
OtherVar
通过单个对象。为此我编写了以下模块加载器, 分别有2个版本:

import importlib

# --------------------------------------------------------------------------------------------------------------------------
    
class Module_Loader( ) :

    __Instance = None

    # --------------------------------------------------------------------------       
    
    def __new__( Self , *Args , **KWT_Args ) :      
        
        if Self.__Instance is None :
                     
            Self.__Instance = super( ).__new__( Self )       

        return Self.__Instance
    
    # --------------------------------------------------------------------------       
    
    def __init__( Self  ) :
    
        pass

    # --------------------------------------------------------------------------   

    def Performer_Instance( Self ) :

        Module_Specs = importlib.util.spec_from_file_location( 'sampler' , 'sampler.py' )     
        Module_Instance = importlib.util.module_from_spec( Module_Specs )
        
        Module_Specs.loader.exec_module( Module_Instance )

        return( Module_Instance )

    # --------------------------------------------------------------------------   

    def Performer_Attributes( Self ) :
                    
        Module_Specs = importlib.util.spec_from_file_location( 'sampler' , 'sampler.py' )     
        Module_Instance = importlib.util.module_from_spec( Module_Specs )
        
        Module_Specs.loader.exec_module( Module_Instance )
        
        Module_Attributes = getattr( Module_Instance , 'myapp' )( ) 

        return( Module_Attributes )

如果我现在加载模块加载器,例如通过启动脚本,我可以 可以打电话给

Performer_Instance( )
,但随后我必须加载个人 加载单个类:

开始.py

import Modules

Test = Modules.Module_Loader( )

example = Test.Performer_Instance( )

myapp = example.myapp( )
Testumgebung = example.Testumgebung( )

myapp.Calculate( )
print( myapp.Test )
print( Testumgebung.Nothing )
print( Testumgebung.OtherVar )

Testumgebung.Neu( )

如果我调用´´´Performer_Attributes( )```,我可以以简单的方式调用方法和属性, 但它仅从``myapp()''加载各个类、方法和属性。

import Modules

Test = Modules.Module_Loader( )

example2 = Test.Performer_Attributes( )

example2.Calculate( )
print( example2.Test )

try :
    example2.Testumgebung( )
except AttributeError :
    print( f"'Testumgebung( )' Could Not Be Found, Because It's Not Inside Of 'myapp( )'" ) 
    
try :
    print( example2.OtherVar )
except AttributeError :
    print( f"'The Variable 'OtherVar' Could Not Be Found, Because It's Not Inside Of 'myapp( )'" )     
    

我意识到这是行不通的,因为

Module_Attributes = getattr( Module_Instance , 'myapp' )( )
直接指的是类
myapp( )

如何在一个对象中传递模块的所有类、方法和属性?

提前致谢!

NumeroUnoDE

python class merge attributes
1个回答
0
投票

我想我发现,需要一个 @classmethod 定义 在没有类对象的情况下调用函数,但不幸的是我没有 知道如何处理。

© www.soinside.com 2019 - 2024. All rights reserved.