如何使 main.py 中的 Class 实例在另一个文件中可用?

How to make Class instance in main.py available in another file?

我在需要在另一个文件的 main.py 中实例化的 class 中提供信息时遇到困难。可以在下面的流程图中看到描述我正在尝试做的事情的最佳方式:

您可以想象的问题是循环依赖。有没有办法在 schema.pymain.py 之间创建一个接口,以便我可以传递 class 信息?

感谢您抽出宝贵时间和提供的任何帮助!

编辑:添加代码供参考

ws_transport.py

from autobahn.twisted.websocket import (
    WebSocketServerProtocol,
    WebSocketServerFactory,
)

from schema import schema


class WsProtocol(WebSocketServerProtocol):
  def __init__(self):
      # Code here

  def onConnect(self, request):
      # Code here

  def onMessage(self, payload, isBinary):
    # Code here


class WsProtocolFactory(WebSocketServerFactory):
  def __init__(self):
    super(WsProtocolFactory, self).__init__()
    self.connection_subscriptions = defaultdict(set)
    # Code here

  def check_events_db(self):
    # Code here

  def check_audit_log_db(self):
    # Code here

web_transport.py

import sys, os
import json

from twisted.web.resource import Resource
from twisted.web.server import Site, http

from schema import schema


class HttpResource(Resource):
  isLeaf = True

  def render_OPTIONS(self, request):
    # Code here

  def render_GET(self, request):
    # Code here

  def render_POST(self, request):
    # Code here



class LoginResource(Resource):
    isLeaf = True

    def render_OPTIONS(self, request):
      # Code here

    def render_GET(self, request):
      # Code here

    def render_POST(self, request):
      # Code here



class RefreshResource(Resource):
  isLeaf = True

  def render_OPTIONS(self, request):
    # Code here

  def render_GET(self, request):
  # Code here

  def render_POST(self, request):
    # Code here



class HttpFactory(Site):

  def __init__(self, resource):
    # Code here

schema.py

#!/usr/bin/python
import graphene
import json
import sys, os

from main import factory


class Query(graphene.ObjectType):
  # Code here


class Mutation(graphene.ObjectType):
  # Code here


class Subscription(graphene.ObjectType):
  # Code here


schema = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription)

main.py

import sys

from twisted.internet import reactor
from twisted.web.resource import Resource
from autobahn.twisted.resource import WebSocketResource

from ws_transport import WsProtocol, WsProtocolFactory
from web_transport import HttpResource, LoginResource, RefreshResource, HttpFactory


if __name__ == '__main__':
    factory = WsProtocolFactory()
    factory.protocol = WsProtocol
    ws_resource = WebSocketResource(factory)

    root = Resource()
    root.putChild("", HttpResource())
    root.putChild("login", LoginResource())
    root.putChild("refresh", RefreshResource())
    root.putChild(b"ws", ws_resource)

    site = HttpFactory(root)

    reactor.listenTCP(8000, site)

    reactor.run()

干杯,

布莱恩

对于 Python 函数,您可以这样做。

def functionInSchema(objectParameter):
    # Add in code

这将使您能够访问 main.py 文件中的对象。 要访问 attributes/methods,请使用参数名称、一个点和 attribute/function 名称。

def functionInSchema(objectParameter):
     attribute = objectParameter.attribute
     objectParameter.method()

然后在 main.py 中将 Class 实例(对象)作为参数传递。

objectInMain = Class(param1, param2)

functionInSchema(objectInMain)

如果您需要访问 schema.py 中的 class,您可以使用以下导入。

from main.py import ClassName

希望对您有所帮助!祝你好运!

我知道这不一定是您需要的答案。但是我 运行 遇到了同样的问题,对我来说这意味着我错误地构建了项目。意思是 main.py 或 schema.py 做他们不应该做的事情。当然你做了这个项目所以你可以决定做什么,但我的意思是也许你应该抽象得更多。因为我不太了解你想用代码做什么,因为我不知道这些库。

一种简单的 hacky 类型的事情是创建另一个名为 maybe run.py 的文件,然后导入这两个文件和依赖项 - 将 main 注入模式或相反。 另一个不太好的解决方案是创建一个 init() 函数,然后在其余文件初始化后导入另一个文件,因此确保只执行一次导入。

但是 所做的是从概念上检查为什么 main 需要导入(在您的情况下)模式,然后问自己这真的是 main.py 应该做的吗做。如果例如您主要需要为所有其他模块提供模板,那么为什么不创建 templates.py 或 modules.py?或者在我的情况下更好的是创建一个总线系统。这可以允许模块仅在需要时共享所需的数据并公开通用 api。但当然,这只有在您仅共享信息时才有意义。

结论:通常当应用程序设计良好时,您永远不会 运行 进入循环导入。当您这样做时,表明您应该重新考虑如何构建您的程序。