Python 如何处理单独文件中的类,或者它们都应该在一个文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:51:48  来源:igfitidea点击:

How does Python handle classes being in separate files or are they all supposed to be in one file

pythonoop

提问by crackity_jones

I'm working on framework for testing some command line utilities. I want to create some classes to hold the different types of information more easily.

我正在开发用于测试一些命令行实用程序的框架。我想创建一些类来更轻松地保存不同类型的信息。

Python is fairly new to me so I'm not sure how you would handle this. Do you keep all your classes in one file with your main script or can you separate them into their own files and use them in your main script.

Python 对我来说相当新,所以我不确定你会如何处理这个问题。您是将所有类与主脚本一起保存在一个文件中,还是可以将它们分成自己的文件并在主脚本中使用它们。

What is the paradigm for how you create multiple classes and use them in a single script?

创建多个类并在单个脚本中使用它们的范例是什么?

Duplicate of How many python classes should I put in one file

我应该在一个文件中放入多少个 python 类的重复项

回答by S.Lott

"What is the paradigm for how you create multiple classes and use them in a single script?"

“创建多个类并在单个脚本中使用它们的范例是什么?”

Are you asking about the importstatement?

你问的是import语句吗?

回答by crackity_jones

An answerfrom the duplicate question in the comments seems to answer my question. My understanding now is that you can add multiple classes to a separate file which would then be referred to as a module. Then you can import that module to use your classes.

回答从注释重复的问题似乎回答我的问题。我现在的理解是,您可以将多个类添加到单独的文件中,然后将其称为模块。然后您可以导入该模块以使用您的类。

回答by Joe Skora

I spread functionality out into separate files as it makes sense, using a modular approach.

我使用模块化方法将功能分散到单独的文件中,因为它有意义。