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

编程入门 行业动态 更新时间:2024-10-24 08:25:25
本文介绍了Python将多个变量初始化为同一个初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已经解决了这些问题,

<块引用>

Python 将多个变量分配给相同的值?列出行为
关于元组,我只想变量可以是字符串、整数或字典更优雅的多变量声明方式同时
这个问题有一些我想问的,但接受的答案非常复杂

所以我正在努力实现,

我声明了这个变量,我想把这个声明减少到尽可能少的代码行.

details = Noneproduct_base = 无product_identity = 无category_string = 无store_id = 无image_hash = 无image_link_mask = 无结果 = 无中止 = 错误数据 = {}

什么是最简单、易于维护的?

解决方案

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

None 对象是单例对象.将 None 对象分配给变量多少次,使用了相同的对象.所以

x = 无y = 无

等于

x = y = 无

但是你不应该对 python 中的任何其他对象做同样的事情.例如,

x = {} # 每次创建一个dict对象y = {}

不等于

x = y = {} # 分配给 x ,y 的相同 dict 对象.我们不应该这样做.

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

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 ?

解决方案

I agree with the other answers but would like to explain the important point here.

None object is singleton object. How many times you assign None object to a variable, same object is used. So

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,

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.

这篇关于Python将多个变量初始化为同一个初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 07:22:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   初始值   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!