Python module dependency

后端 未结 5 1277
耶瑟儿~
耶瑟儿~ 2021-02-02 13:57

Ok I have two modules, each containing a class, the problem is their classes reference each other.

Lets say for example I had a room module and a person module containin

5条回答
  •  一生所求
    2021-02-02 14:38

    No need to import CRoom

    You don't use CRoom in person.py, so don't import it. Due to dynamic binding, Python doesn't need to "see all class definitions at compile time".

    If you actually do use CRoom in person.py, then change from room import CRoom to import room and use module-qualified form room.CRoom. See Effbot's Circular Imports for details.

    Sidenote: you probably have an error in Self.NextId += 1 line. It increments NextId of instance, not NextId of class. To increment class's counter use CRoom.NextId += 1 or Self.__class__.NextId += 1.

提交回复
热议问题