Python-定义常量变量的最方便方法

2024-09-28 17:00:16 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个文件“icon.ico”和一个url“url.com”。
只在类中使用一次-“icon.ico”将设置为某个窗口,我们将用一种方法请求url。
我有三种方法来定义这些变量。

第1种方式-定义为全局常量

#in the top of the file
ICON = "icon.ico"
URL = "http://url.com"

#and then
def setIcon(self):
    self.setWindowIcon(QtGui.QIcon(ICON))

def getData(self):
    content = requests.get(URL).content

第二种方式-定义为类的变量

def __init__(self):
    self.url = "http://url.com"
    self.icon = "icon.ico"

第三方-仅在将要使用的方法内定义

def setIcon(self):
    icon = "icon.ico"

def getData(self):
    url = "http://url.com"

Tags: the方法selfcomhttpurl定义def
3条回答

经验法则

  • 一般来说,您应该避免全局变量,因为它们存在于内存中,因为您导入模块直到程序完成(第一种情况)
  • 一般来说,您应该避免在函数(第二和第三种情况)内修复值,因为它使函数les可重用。

而不是:

def __init__(self):
    self.url = "http://url.com"
    self.icon = "icon.ico"

或者

def setIcon(self):
    icon = "icon.ico"

更喜欢:

def __init__(self, url, icon):
    self.url = url
    self.icon = icon

或者,如果您认为这些值将是相同的90%:

def __init__(self, url="http://url.com", icon="icon.ico"):
    self.url = url
    self.icon = icon

何时使用每个案例的提示

第1种方式-定义为全局常量

  • 常量具有作为模块范围常量的意义。请记住,可以在同一个模块中声明多个类和函数。这意味着常量将被用于整个模块,并且它不属于任何特定的类。
  • 你需要快速找到常数,通常是为了改变它的值。在这种情况下,您可能不需要常量,而是需要变量。

第二种方式-定义为类的变量

  • 如果它是类的变量,则它不是常量。如果要使用类的常量或变量(在类级别而不是实例级别),则应使用第4种方式-作为类级别常量。
  • 如果需要实例级常量或变量,则应使用2dn经验法则

第三方-仅在将要使用的方法内定义

  • 你应该避免这样做,而采用第二条经验法则

4路-作为类级常量

  • 仅对共享同一类的所有实例的变量和常量使用建议的方法,这实际上意味着类级别或类范围

将它们定义为类变量可能是最有前途的方法,因为您以后可以使用dependency injection来更改这些变量,这对unit testing非常有用。例如:

class Server:
    def __init__(self, url, icon):
        self.url = url
        self.icon = icon


server = Server('url.com', 'file.ico')

# in your tests, you may want to use a different ico/url
test_server = Server('url.com', 'test_icon.ico')

关于接二连三的注记:

还要注意的是,在Python中,getter和setter倾向于避免使用,如果需要验证,则使用properties代替,或者重构具有大量依赖代码的类。在Java和C等预编译语言中,getter/setter用于封装,以便以后可以更改实现,但在Python中,为了提高性能和清晰度,避免使用getter/setter。因此,首先,变量可以正常访问,如果实现发生更改,那么可以使用@property@variable.setter修饰符,以便使用getter和setter,即使您看起来是直接访问变量。

因此,最初您可以直接访问icon

print(server.icon)

但让我们说,稍后重构类中的icon_icon_file_icon_image,并在每次设置时加载文件,但应用程序的其余部分需要icon变量。这就是getter和setter通常的用途(以及对设置变量的任何检查/转换),因此我们现在可以为icon添加getter和setter,即使icon变量不再存在:

class Server:
    def __init__(self, url, icon_filename):
        self.url = url
        self._icon_filename = icon_filename
        self._icon_image = self._load_icon(icon_filename)

    @property
    def icon(self):
        """
        Get the icon file name
        @returns str of the icon filename
        """
        return self._icon_filename

    @icon.setter
    def icon(self, icon_filename):
        """
        Load a new icon file as the icon
        @param icon_filename the relative path to the icon file
        """
        if icon_filename[-4:] != '.ico':
            raise Exception('Must be of .ico format')

        self._icon_filename = icon_filename
        self._icon_image = self._load_icon(icon_filename)

    def _load_icon(self, icon_filename):
        """
        Load a .ico file, and maybe do some other stuff
        @returns Image of the loaded icon
        @private
        """
        # implementation here...       


server = Server('url.com', 'file.ico')
print(server.icon)  # file.ico
server.icon = 'icon2.ico'  # sets and loads new icon
print(server.icon)  # icon2.ico
server.icon = 'icon3.jpg'  # throws an exception

您忘记了将它们定义为类级常量的选项:

class Foo(...)
    ICON = "xxx"
    URL = "YYY"

相关问题 更多 >