简体   繁体   English

是否有 Python 版本的 .h 和 .cpp 文件?

[英]Is there a Python version of .h and .cpp files?

In C++ there are .h and .cpp files for design and implementation of code respectively.在 C++ 中有 .h 和 .cpp 文件分别用于代码的设计和实现。 Is there a similar way to structure files in Python?有没有类似的方式在 Python 中构建文件? Can I define my class in one file, and then put all the code for that class in another?我可以在一个文件中定义我的类,然后将该类的所有代码放在另一个文件中吗?

I feel like the answer is "no".我觉得答案是“不”。 I've tried looking up the answer, but the closest results were just how to merge c++ and python.我试过查找答案,但最接近的结果就是如何合并 c++ 和 python。

It would be much less worthwhile to do than in c++ because as AlexG mention python is not a compiled language.与在 C++ 中相比,这样做的价值要低得多,因为正如 AlexG 所说,python 不是一种编译语言。 However, you could achieve a "similar" effect by linking to another python file.但是,您可以通过链接到另一个 python 文件来实现“类似”的效果。 For example, if you wanted to have all of your functions created in a single file, and all of your logic done using those functions in another you would simply use import filename and you could reference your functions in the second file like you would in a .h and .cpp file.例如,如果您希望在单个文件中创建所有函数,并且在另一个import filename使用这些函数完成所有逻辑,则只需使用import filename即可在第二个文件中引用您的函数,就像在.h 和 .cpp 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM