代码样式1:
from ..service.user_service import UserService
class UserView:
user_service = UserService()
user_service.get_all_users()
# rest of code
代码样式2:
from ..service.user_service import UserService as userService
class UserView:
userService.get_all_users()
# rest of code
service.user_service.py
from .models.user import User # please
class UserService:
def get_all_users(self):
return User.objects.all()
两个代码都可以正常工作,但是哪种方法正确?我也想知道这是否可能引起circular import error
?我遇到了这个问题Circular (or cyclic) imports in Python。
style 2
代码的工作机制是什么?