Python将多个变量初始化为同一个初始值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33331732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python initialize multiple variables to the same initial value
提问by Rivadiz
I have gone through these questions,
我经历过这些问题,
- Python assigning multiple variables to same value? list behavior
concerned with tuples, I want just variables may be a string, integer or dictionary- More elegant way of declaring multiple variables at the same time
The question has something I want to ask, but the accepted answer is much complex
- Python将多个变量分配给相同的值?列出与元组有关的行为
,我只希望变量可以是字符串、整数或字典- 更优雅的同时声明多个变量的方法
这个问题有我想问的,但接受的答案很复杂
so what I'm trying to achieve,
所以我正在努力实现的目标
I have this variables declared, and I want to reduce this declarations to as less line of code as possible.
我声明了这个变量,我想把这个声明减少到尽可能少的代码行。
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 = {}
What is the simplest, easy to maintain ?
什么是最简单、易于维护的?
采纳答案by Shan
I agree with the other answers but would like to explain the important point here.
我同意其他答案,但想在这里解释重要的一点。
Noneobject is singleton object. How many times you assign None object to a variable, same object is used. So
None对象是单例对象。将 None 对象分配给变量多少次,使用了相同的对象。所以
x = None
y = None
is equal to
等于
x = y = None
but you should not do the same thing with any other object in python. For example,
但是你不应该对 python 中的任何其他对象做同样的事情。例如,
x = {} # each time a dict object is created
y = {}
is not equal to
不等于
x = y = {} # same dict object assigned to x ,y. We should not do this.
回答by DasFranck
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 = {}
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 = {}
That's how I do.
我就是这样做的。
回答by Aske Doerge
First of all I would advice you not to do this. It's unreadable and un-Pythonic. However you can reduce the number of lines with something like:
首先,我建议你不要这样做。它不可读且非 Pythonic。但是,您可以通过以下方式减少行数:
details, product_base, product_identity, category_string, store_id, image_hash, image_link_mask, results = [None] * 8
abort = False
data = {}
回答by jerblack
I have a one-line lambda function I use that helps me out with this.
我有一个单行 lambda 函数可以帮助我解决这个问题。
nones = lambda n: [None for _ in range(n)]
v, w, x, y, z = nones(5)
The lambda is the same thing as this.
lambda 与此相同。
def nones(n):
return [None for _ in range(n)]
回答by Stan
This does not directly answer the question, but it is related -- I use an instance of an empty class to group similar attributes, so I do not have to clutter up my initmethod by listing them all.
这并没有直接回答问题,但它是相关的——我使用一个空类的实例来对相似的属性进行分组,所以我不必通过列出所有的init方法来弄乱我的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)
What is the difference between SimpleNamespace and empty class definition?
回答by belgacea
A mix of previous answers :
先前答案的混合:
def default(value, number):
return [value] * number
v, w, x, y, z = default(20, 5)