简体   繁体   English

关于 Python 中导入对象的约定的问题?

[英]Question on the convention of importing objects in Python?

Suppose I have a project in ~/app/, containing at least files myclass.py, myobject.py, and app.py.假设我在 ~/app/ 中有一个项目,至少包含文件 myclass.py、myobject.py 和 app.py。

In myclass.py I have something like在 myclass.py 我有类似的东西

def myclass():
    # class attributes and methods...

In myobject.py, I have something like在 myobject.py 中,我有类似的东西

from app import myclass
attribute1 = 1
attribute2 = 2
myobject = myclass(attribute1, attribute2)

Finally, app.py looks something like最后,app.py 看起来像

from app import myobject
# do stuff with myobject

In practice, I'm using myobject.py to gather a common instance of myclass and make it easily importable, so I don't have to define all the attributes separately.实际上,我使用 myobject.py 来收集 myclass 的通用实例并使其易于导入,因此我不必单独定义所有属性。 My question is on the convention of myobject.py.我的问题是关于 myobject.py 的惯例。 Is this okay or is there something that would be better to achieve the purpose mentioned.这样可以吗,或者有什么可以更好地实现所提到的目的。 The concerns I thought of is that there are all these other variables (in this case attribute1 and attribute2) which are just... there... in the myobject module.我想到的问题是,所有这些其他变量(在本例中为 attribute1 和 attribute2)只是......在那里......在 myobject 模块中。 It just feels a little weird because these aren't things that would ever be accessed individually, but the fact that it is accessible... I feel like there's some other conventional way to do this.只是感觉有点奇怪,因为这些不是单独访问的东西,但事实上它是可访问的……我觉得还有其他一些传统的方法可以做到这一点。 Is this perfectly fine, or am I right to have concerns (if so, how to fix it)?这完全没问题,还是我有顾虑(如果是,如何解决)?

Edit: To make it more clear, here is an example: I have a Camera class which stores the properties of the lens and CCD and such (like in myclass.py).编辑:为了更清楚,这里有一个例子:我有一个相机 class,它存储镜头和 CCD 等的属性(比如在 myclass.py 中)。 So users are able to define different cameras and use them in the application.因此用户可以定义不同的相机并在应用程序中使用它们。 However, I want to allow them to have some preset cameras, thus I define objects of the Camera class that are specific to certain cameras I know are common to use for this application (like in myobject.py).但是,我想让他们有一些预设相机,因此我定义了相机 class 的对象,这些对象特定于我知道通常用于此应用程序的某些相机(如在 myobject.py 中)。 So when they run the application, they can just import these preset cameras (as Camera objects) (like in app.py).所以当他们运行应用程序时,他们可以只导入这些预设相机(作为相机对象)(就像在 app.py 中一样)。 How should these preset objects be written, if how it's written in myobject.py is not the best way?如果在myobject.py中这样写不是最好的方法,那么这些预设对象应该怎么写呢?

在此处输入图像描述

so you this method fails to call function inside the class in first case.所以你这个方法在第一种情况下无法在 class 中调用 function。 i think you do it by making a class of attribute and getting variables from it.我想你是通过制作一个 class 的属性并从中获取变量来实现的。

class Attribute():
     def __init(self,a1,a2):
         self.a1=a1
         self.a2=a2
att=Attribute(1,2)
print(att.a1)

It looks like you stumbled upon the singleton pattern.看起来您偶然发现了singleton模式。 Essentially, your class should only ever have one instance at any time, most likely to store global configurations or some similar purpose.本质上,您的 class 在任何时候都应该只有一个实例,最有可能存储全局配置或某些类似目的。 In Java, you'd implement this pattern by making the constructor private, and have a static method (eg. getInstance() ) that returns a private static instance.在 Java 中,您将通过将构造函数设为私有来实现此模式,并有一个返回私有 static 实例的 static 方法(例如getInstance() )。

For Python, it's actually quite tricky to implement singletons.对于Python,单例的实现其实还是比较棘手的。 You can see some discussion about that subject here .您可以在此处看到有关该主题的一些讨论。 To me how you're doing it is probably the simplest way to do it, and although it doesn't strictly enforce the singleton constraint, it's probably better for a small project than adding a ton of complexity like metaclasses to make 'true' singletons.对我来说,你是怎么做的可能是最简单的方法,虽然它没有严格执行 singleton 约束,但对于一个小项目来说,它可能比添加大量复杂性(如元类)来制作“真正的”单身人士更好.

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

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