Python:如何从类所在的范围中获取常量?

时间:2022-09-15 13:11:46

I want to use the value of a constant defined in django's settings.py inside a class.

我想使用django的设置中定义的常量的值。py中的类。

I know I can from myprojectname import settings but I want to make it a python package so it'd be better if I could get those values from the scope the class is instantiated.

我知道我可以从myprojectname导入设置,但是我想把它变成一个python包,所以如果我能从类实例化的范围内得到那些值就更好了。

Passing it to every instance would be inconvenient since these constants would be used throughout an entire project.

将其传递给每个实例将不方便,因为这些常量将在整个项目中使用。

Is there a way to do that?

有办法吗?

1 个解决方案

#1


1  

Generally speaking, you want to have as few dependencies as possible.

一般来说,您希望拥有尽可能少的依赖项。

Since I know no way to use variables from the context a class was instantiated in, I would prefer to do something like that:

由于我不知道如何使用实例化类的上下文中的变量,所以我宁愿这样做:

class Independent(object):

  def __init__(self, context):
     self._context = context

Thus the instantiating code can provide a context object that can contain any context information you need to have for operation. This would make the class totally independent from the context -- but comes with a price of course, since you must provide the information at any instantiation. But you merely need only one such object for any context you have, so the memory overhead is limited to one additional property.

因此,实例化代码可以提供一个上下文对象,该对象可以包含操作所需的任何上下文信息。这将使类完全独立于上下文—但是当然要付出代价,因为您必须在任何实例化中提供信息。但是对于您拥有的任何上下文,您只需要一个这样的对象,因此内存开销仅限于一个附加属性。

An other option would be, that the module has a global context variable. When you import the module, you can set the value as needed in one of your more specific modules, but you should do it in the modules coding of course. All the classes in the module can use this global variable. Of course, in any program you are limited than to one context for the whole module, but in different programs you can have different contexts and your module is not depending on some specifics like "Django".

另一个选项是,模块有一个全局上下文变量。当您导入模块时,您可以根据需要在一个更特定的模块中设置值,但是您当然应该在模块编码中这样做。模块中的所有类都可以使用这个全局变量。当然,在任何程序中,整个模块的上下文都是有限的,但是在不同的程序中,您可以有不同的上下文,并且您的模块不依赖于某些特定的内容,比如“Django”。

#1


1  

Generally speaking, you want to have as few dependencies as possible.

一般来说,您希望拥有尽可能少的依赖项。

Since I know no way to use variables from the context a class was instantiated in, I would prefer to do something like that:

由于我不知道如何使用实例化类的上下文中的变量,所以我宁愿这样做:

class Independent(object):

  def __init__(self, context):
     self._context = context

Thus the instantiating code can provide a context object that can contain any context information you need to have for operation. This would make the class totally independent from the context -- but comes with a price of course, since you must provide the information at any instantiation. But you merely need only one such object for any context you have, so the memory overhead is limited to one additional property.

因此,实例化代码可以提供一个上下文对象,该对象可以包含操作所需的任何上下文信息。这将使类完全独立于上下文—但是当然要付出代价,因为您必须在任何实例化中提供信息。但是对于您拥有的任何上下文,您只需要一个这样的对象,因此内存开销仅限于一个附加属性。

An other option would be, that the module has a global context variable. When you import the module, you can set the value as needed in one of your more specific modules, but you should do it in the modules coding of course. All the classes in the module can use this global variable. Of course, in any program you are limited than to one context for the whole module, but in different programs you can have different contexts and your module is not depending on some specifics like "Django".

另一个选项是,模块有一个全局上下文变量。当您导入模块时,您可以根据需要在一个更特定的模块中设置值,但是您当然应该在模块编码中这样做。模块中的所有类都可以使用这个全局变量。当然,在任何程序中,整个模块的上下文都是有限的,但是在不同的程序中,您可以有不同的上下文,并且您的模块不依赖于某些特定的内容,比如“Django”。