Python module dependency

后端 未结 5 1278
耶瑟儿~
耶瑟儿~ 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:22

    @S.Lott if i don't import anything into the room module I get an undefined error instead (I imported it into the main module like you showed)

    Traceback (most recent call last):
    File "C:\Projects\python\test\main.py", line 6, in
    Ben = Room.AddPerson('Ben', 'Blacker', 'Male')
    File "C:\Projects\python\test\room.py", line 12, in AddPerson
    Person = CPerson(FirstName,SecondName,Gender,Id)
    NameError: global name 'CPerson' is not defined

    Also, the reason there diffrent modules is where I encountered the problem to start with the container class (ieg the room) is already several hundred lines, so I wanted the items in it (eg the people) in a seperate file.

    EDIT: main.py

    from room import CRoom
    from person import CPerson
    
    Room = CRoom()
    
    Ben = Room.AddPerson('Ben', 'Blacker', 'Male')
    Tom = Room.AddPerson('Tom', 'Smith',   'Male')
    
    Ben.Leave()
    

提交回复
热议问题