Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用类存储多个函数所需的变量是否合理?_Python_Class_Python Winshell - Fatal编程技术网

Python 使用类存储多个函数所需的变量是否合理?

Python 使用类存储多个函数所需的变量是否合理?,python,class,python-winshell,Python,Class,Python Winshell,我正在编写一个python脚本,它的一个特性应该是自动将自己添加到windows启动文件夹中 现在我写了一些函数,它们都依赖于多个变量,这些变量在初始赋值后不会改变。但我不知道把它们放在哪里 这是代码片段: def write_shortcut(): autostart_folder = winshell.startup() path = os.path.abspath(__file__) target_folder = autostart_folder + r"\pro

我正在编写一个python脚本,它的一个特性应该是自动将自己添加到windows启动文件夹中

现在我写了一些函数,它们都依赖于多个变量,这些变量在初始赋值后不会改变。但我不知道把它们放在哪里

这是代码片段:

def write_shortcut():
    autostart_folder = winshell.startup()
    path = os.path.abspath(__file__)
    target_folder = autostart_folder + r"\proxy-changer.lnk"
    working_directory = path.replace(r'\proxy-changer.pyw', '')

    with winshell.shortcut() as shortcut:
        shortcut.path = path
        shortcut.working_directory = working_directory
        shortcut.description = "Shortcut to the proxy-changer script"
        shortcut.write(target_folder)

def del_shortcut():
    os.remove(target_folder)

def check_shortcut():
    if (config.getboolean('DEFAULT','RunOnStartup') == 1 and not
        os.path.islink(target_folder)):
        write_shortcut()
    elif (config.getboolean('DEFAULT','RunOnStartup') == 0 and
          os.path.islink(target_folder)):
        del_shortcut()
    else:
        pass
第2行到第5行是我要说的变量。 目前它们处于第一个功能中,但其他功能无法访问它们。现在我知道类是存在的,类中的所有方法都可以访问类中的变量,但我不确定这是否正确,因为它只包含一个对象,而不是多个对象

在函数之外,甚至在定义它们之前,看起来也不是很干净。我不确定是否要让它们全球化

我想我大体上理解课堂,但不是所有的东西,所以如果很明显课堂是正确的方式,那么我很抱歉。我只是听说类经常在不需要的时候使用,所以我尽量避免犯同样的错误。 我是python初学者,可以随意批评我的代码风格,也可以告诉我哪里错了/可以改进它


提前感谢。

将常量作为模块/脚本的全局变量是非常好的。请注意,对于常量使用大写名称是惯用做法

常数

常量通常在模块级别定义并写入 所有大写字母,用下划线分隔单词。例子 包括最大溢出和总计

例如,您可以在函数前面定义常量:

PATH = os.path.abspath(__file__)
TARGET_FOLDER = winshell.startup() + r"\proxy-changer.lnk"
WORKING_DIRECTORY = os.path.dirname(PATH)

def write_shortcut():
    with winshell.shortcut() as shortcut:
        shortcut.path = PATH
        shortcut.working_directory = WORKING_DIRECTORY
        shortcut.description = "Shortcut to the proxy-changer script"
        shortcut.write(TARGET_FOLDER)

def del_shortcut():
    os.remove(TARGET_FOLDER)

使用类不会给您带来任何好处。缺点是它模糊了您的意图,通常速度较慢,并且无法提供通常与类相关的功能

最直接的方法是将常量添加为类属性:

class Constants:
    path = os.path.abspath(__file__)
    target_folder = winshell.startup() + r"\proxy-changer.lnk"
    working_directory = os.path.dirname(path)

def del_shortcut():
    os.remove(Constants.target_folder)

请注意,
Constants
的行为与带有全局变量的模块完全相同,只是它仍然具有类的所有现在无用的功能。例如,虽然可以实例化
常量
,但这是完全没有意义的,因为实例既没有状态也没有方法。

因为它们是常量,而不是变量,所以将它们放在全局范围是完全正确的。添加一个类来保存它们很可能意味着它们是类属性,或者该类是一个单例,其行为基本上与带有全局变量的模块完全相同——只是您必须手动设置所有这些。您的代码无法工作。在函数内声明作用域变量-在函数外它们是不可访问的。如果在开始时有不同的上下文,您可能希望使用一个“上下文”类,该类包含您初始化为值并使用它们的类变量。如果它们从未改变,就把它们放在全球范围内。如果用户可以通过配置文件或其他方式指定它们:创建一类设置,加载它们并将其实例传递给函数。传递数据的方法很多。由于您使用的不是创建最终常量,而是
autostart\u文件夹
,因此我将其直接包含在常量定义中。如果以后需要它,也可以为它定义一个单独的常量。