V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chenqh
V2EX  ›  Python

关于 py 设计的一些问题

  •  
  •   chenqh · 2020-01-07 14:44:53 +08:00 · 2570 次点击
    这是一个创建于 1570 天前的主题,其中的信息可能已经有所发展或是发生改变。

    1.假如我有个service_开关的文件列表
    这些service_文件里面大概就是这样的

    class ServiceA(object):
    	def __init__(self):
        	pass
    
    

    有一个service_manager

    class ServiceManager(object):
    	property
        def service_a(self):
        	if not hasattr(self, '_service_a'):
            	self._service_a =ServiceA()
        	return self._service_a
        
        
    

    但是现在有一个问题我在 service_a 想通过service_manager调用 service_b 的方法
    我现在是在 service_a 的初始化过程中

    class ServiceA(object):
    	def __init__(self, service_manager):
        	self.service_manager = service_manager
    
    

    但是这个样子怪怪的,而且这个样子我在 service_a 应该是无法标注 service_manager 的类型
    因为会循环报错
    这个样子应该怎么处理?

    12 条回复    2020-01-10 09:45:07 +08:00
    2379920898
        1
    2379920898  
       2020-01-07 14:49:10 +08:00   ❤️ 1
    使用函数处理字符串
    2379920898
        2
    2379920898  
       2020-01-07 14:49:15 +08:00
    EASY
    chenqh
        3
    chenqh  
    OP
       2020-01-07 14:51:32 +08:00
    @2379920898 函数处理字符串?这是什么?
    2379920898
        4
    2379920898  
       2020-01-07 14:52:10 +08:00
    敏捷开发
    BingoXuan
        5
    BingoXuan  
       2020-01-07 14:54:06 +08:00   ❤️ 1
    先定义个 ServiceManagerBase 的类,定义继承 ServiceManagerBase 的类 Manager,定义 ServiceA 的类,其入参类型是 ServiceManager。
    robot1
        6
    robot1  
       2020-01-07 15:20:40 +08:00   ❤️ 1
    楼上没错 给 manager 加个接口类啊。这就是传说中的依赖注入吧
    robot1
        7
    robot1  
       2020-01-07 15:23:02 +08:00
    python 有个 zope 库可实现空接口
    oahebky
        8
    oahebky  
       2020-01-07 16:31:38 +08:00
    出现循环的情况,跟你标没标注没有关系。

    class ServiceManager(object):
    @ property
    def service_a(self):
    if not hasattr(self, '_service_a'):
    self._service_a =ServiceA(self)
    return self._service_a


    class ServiceA(object):
    def __init__(self, service_manager):
    self.service_manager = service_manager

    #### Usage:
    mgr = ServiceManager()
    ser_a = ServiceA(mgr)
    obj_1 = ser_a.service_manager.service_a

    》》》 ser_a != obj_1

    obj_2 = obj_1.service_manage.service_a

    》》》 obj_1 == obj_2 != ser_a

    #### Usage:
    mgr = ServiceManager()
    ser_a1 = mgr.service_a
    ser_a2 = ser_a1.service_manager.service_a

    》》》 ser_a1 == ser_a2


    不过基本上这么设计最后都会放弃。
    暂时可以先用着,用着用着或许就知道怎么重新设计了。
    chenqh
        9
    chenqh  
    OP
       2020-01-07 19:20:30 +08:00
    @oahebky 我知道有循环引用,但是加了 typing hint 直接会报错,运行不了,也可以不改我现在的代码,就是没有智能提示
    ctrlaltdeletel
        10
    ctrlaltdeletel  
       2020-01-07 19:35:52 +08:00   ❤️ 1
    感觉现在依赖关系有点混乱
    如果单纯想加上 type hint,可以结合 typing.TYPE_CHECKING 和 forwardref 试试
    chenqh
        11
    chenqh  
    OP
       2020-01-07 19:39:02 +08:00
    @ctrlaltdeletel 加了,但是 vscode 好像还是没有智能提示,有点郁闷
    chenqh
        12
    chenqh  
    OP
       2020-01-10 09:45:07 +08:00
    @robot1 刚刚有想了下, 加个接口类,这样也没有智能提示呢,这样和我现在没有区别呀
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:31 · PVG 05:31 · LAX 14:31 · JFK 17:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.