Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables_Variable Assignment - Fatal编程技术网

Python将多个变量初始化为相同的初始值

Python将多个变量初始化为相同的初始值,python,variables,variable-assignment,Python,Variables,Variable Assignment,我已经回答了这些问题 关于元组,我只希望变量可以是字符串、整数或字典 我想问这个问题,但公认的答案非常复杂 所以我想要实现的是 我声明了这些变量,我想将这些声明减少到尽可能少的代码行 details = None product_base = None product_identity = None category_string = None store_id = None image_hash = None image_link_mask = None results = None ab

我已经回答了这些问题


  • 关于元组,我只希望变量可以是字符串、整数或字典

  • 我想问这个问题,但公认的答案非常复杂
  • 所以我想要实现的是

    我声明了这些变量,我想将这些声明减少到尽可能少的代码行

    details = None
    product_base = None
    product_identity = None
    category_string = None
    store_id = None
    image_hash = None
    image_link_mask = None
    results = None
    abort = False
    data = {}
    
    什么是最简单、易于维护的

    (
        details,
        producy_base,
        product_identity,
        category_string,
        store_id,
        image_hash,
        image_link_mask,
        results,
    ) = (None, None, None, None, None, None, None, None)
    
    abort = False
    data = {}
    

    我就是这么做的。

    首先,我建议你不要这样做。这是不可读的,不符合Python的。但是,您可以使用以下方法减少行数:

    details, product_base, product_identity, category_string, store_id, image_hash, image_link_mask, results = [None] * 8
    abort = False
    data = {}
    

    我同意其他答案,但想在此解释一下重点

    None对象是单例对象。将无对象分配给变量的次数,即使用相同的对象。所以

    x = None
    y = None
    
    等于

    x = y = None
    
    但是您不应该对python中的任何其他对象执行相同的操作。比如说,

    x = {}  # each time a dict object is created
    y = {}
    
    不等于

    x = y = {}  # same dict object assigned to x ,y. We should not do this.
    

    我使用一行lambda函数来帮助我解决这个问题

    nones = lambda n: [None for _ in range(n)]
    v, w, x, y, z = nones(5)
    
    lambda和这个是一样的

    def nones(n):
        return [None for _ in range(n)]
    

    这并不能直接回答这个问题,但它是相关的——我使用一个空类的实例来对类似的属性进行分组,这样我就不必通过列出所有属性来混乱我的init方法

    class Empty:
        pass
    
    class Application(tk.Frame):
        def __init__(self, master=None):
            super().__init__(master)
            self.w = Empty()          # widgets
            self.master = master
            self.pack()
            self.create_widgets()
    
        def create_widgets(self):
            self.w.entry = tk.Entry(self, bg="orange", fg="black", font=FONT)
    

    之前的答案组合:

    def default(number, value = None):
        return [value] * number
        
    o, p, q, r, s = default(5)
    t, u = default(2, false)
    v, w, x = default(3, {})
    y, z = default(2, 0)
    

    你需要使用字典。这很复杂,我必须调用
    dicitonary['details']
    KeyErrors
    suck.,而且IDE不会突出显示无效键,而是突出显示变量。如果我必须使用
    details=dicitonary['details']
    ,我最好使用
    details=None
    ,而不是这一轮的dict creation、lookup和keyrerrors。你是如何定义复杂的呢?@CrakC请看上面的评论。
    a,b=(True,)*2
    a=b=True
    哇,我不明白这个[None]*8,但它很容易阅读抱歉,我是个noob,使用lambda而不是直接使用它有什么好处吗?a、 b,c=(范围(3)内的uu无)