[英]Creating An Object From a Different File in Python
我正在尝试一个涉及多个文件的项目。 . 这个 object 是两个对象的组合。 这些文件位于 Py Charm 2020.3 上。 我已尝试添加一个init ,如本问题所示( Create Python object from different.py 文件)我收到的错误消息是没有模块。 整个错误消息如下。
Traceback(最近一次调用最后):文件“C:/Users/Aku/ImportExports/Message/TellAndReciveMessage.py”,第 2 行,来自 Tell 导入 TellMessage ModuleNotFoundError:没有名为“Tell”的模块
告诉消息文件:
class TellMessage:
def __init__(self, msg:str) -> None:
self.msg = msg
def show(self) -> None:
print(self.msg)
def main() -> None:
tell_message = TellMessage("hello world")
tell_message.show()
if __name__ == "__main__":
main()
#Tell and Receive Message File
from Message import TellMessage
class TellAndReciveMessage:
def __init__(self, tell:TellMessage, recived:str):
self.tell = tell
self.recived = ""
def get_recived(self) -> str:
return self.recived
def show(self) -> None:
self.tell.show()
self.recived = input(">>")
def main() -> None:
tell = TellMessage("hello world")
both = TellAndReciveMessage(tell, "")
tell.show()
print(tell.get_recived())
if __name__ == "__main__":
main()
``
''''
"""" package 的初始化模块 """
"""告诉消息文件"""
class TellMessage: def init (self, msg:str) -> None: self.msg = msg
def show(self) -> None:
"""
Command line output
"""
print(self.msg)
def make(msg:str) -> TellMessage: """:param msg: msg to recive Output: A new Tell and Reicive Class Note: This function can be called by the other class """ return TellMessage(msg)
def main() -> None: """ 输入: None Output: None 注意: 在命令行输出结果 """ tell_message = TellMessage("hello world") tell_message.show()
如果名称== “主”:主()
"""告诉和接收消息模块"""
从消息导入 TellMessage
class TellAndReciveMessage: def init (self, tell:TellMessage, recived:str): self.tell = tell self.recived = ""
def get_recived(self) -> str:
"""
Output: recived string (idenpotent)
"""
return self.recived
def show(self) -> None:
"""
Shows output and gets input
Output: None
"""
if not self.tell:
self.recived = input(">>")
return
self.tell.show()
self.recived = input(">>")
def main() -> None: """ Input: None Output: None 注意:在命令行上运行代码和 output
"""
#Here is the line that fixes it (can call function)
tell = TellMessage.make("hello world")
both = TellAndReciveMessage(None, "")
both.show()
print(both.get_recived())
如果名称== “主”:主()
''''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.