且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python 循环导入再次(也就是这个设计有什么问题)

更新时间:2023-10-24 12:07:58

循环导入本质上并不是一件坏事.team 代码依赖 useruser 使用 team 做某事是很自然的.

Circular imports are not inherently a bad thing. It's natural for the team code to rely on user whilst the user does something with team.

这里更糟糕的做法是from module import member.team 模块正在尝试在导入时获取 user 类,而 user 模块正在尝试获取 team代码>类.但是 team 类还不存在,因为当 user.py 运行时你仍然在 team.py 的第一行.

The worse practice here is from module import member. The team module is trying to get the user class at import-time, and the user module is trying to get the team class. But the team class doesn't exist yet because you're still at the first line of team.py when user.py is run.

相反,只导入模块.这导致更清晰的命名空间,使以后的猴子修补成为可能,并解决了导入问题.因为您只是在导入时导入 module,所以您不必关心其中尚未定义的 class.到你开始使用这个类的时候,它会是.

Instead, import only modules. This results in clearer namespacing, makes later monkey-patching possible, and solves the import problem. Because you're only importing the module at import-time, you don't care than the class inside it isn't defined yet. By the time you get around to using the class, it will be.

所以,test/users.py:

So, test/users.py:

import test.teams

class User:
    def setTeam(self, t):
        if isinstance(t, test.teams.Team):
            self.team = t

test/teams.py:

test/teams.py:

import test.users

class Team:
    def setLeader(self, u):
        if isinstance(u, test.users.User):
            self.leader = u

from test import teams 然后teams.Team 也可以,如果你想少写test.那仍然是导入模块,而不是模块成员.

from test import teams and then teams.Team is also OK, if you want to write test less. That's still importing a module, not a module member.

另外,如果TeamUser比较简单,把它们放在同一个模块里.您不需要遵循 Java 一个文件一个类的习惯用法.isinstance 测试和 set 方法也让我尖叫 unpythonic-Java-wart;取决于你在做什么,你***使用一个普通的、未经类型检查的 @property.

Also, if Team and User are relatively simple, put them in the same module. You don't need to follow the Java one-class-per-file idiom. The isinstance testing and set methods also scream unpythonic-Java-wart to me; depending on what you're doing you may very well be better off using a plain, non-type-checked @property.