简体   繁体   English

将多个值分配给 python 中的单个 int 或 float 或 string 变量。 可能吗?

[英]Assigning multiple values to a single int or float or string variable in python. Is it possible?

Recently I encountered someone's code along the lines of:最近我遇到了某人的代码:

b = {
  'b': "b" or []
}

When I do a print(b) I get {'b': 'b'} .当我执行print(b)时,我得到{'b': 'b'} It got me questioning when will b ever be [] ?这让我质疑b什么时候会是[] Or is it saying b is both "b" and [] ?还是说b既是"b"又是[]

But if that is the case:但如果是这样的话:

a = (1 or 2)
print(a == 1)
print(a == 2)

Why does print(a == 1) results in True but print(a==2) results in False ?为什么print(a == 1)结果是Trueprint(a==2)结果是False

This leads me to a final question, is it possible for a variable of type int or float or string to hold multiple values at the same time?这引出了最后一个问题,int、float 或 string 类型的变量是否可以同时保存多个值? ie, a is both 1 and 2 provided that it is not a list or dictionary?即, a既是1又是2 ,前提是它不是列表或字典?

No, you can't assign multiple values to a single variable.不,您不能将多个值分配给单个变量。

The expression x or y returns y if x is false, or x if not.如果 x 为假,则表达式x or y返回y ,否则返回x

A string only evaluates false if it's empty.一个字符串只有在它为空时才计算为假。 Your string "b" is not empty, so it will never be false.你的字符串"b"不为空,所以它永远不会是假的。 Thus there's no way for that expression "b" or [] to equal [] , it will always be "b" .因此,该表达式"b" or []无法等于[] ,它将始终为"b"

Not, it is not possible.不,这是不可能的。

What you have done is assign to a the value of the expression (1 or 2) ;您所做的是分配给表达式(1 or 2) a值; that is, the result of or -ing 1 and 2.or -ing 1 和 2 的结果。

a will never be 2. (1 or 2) will always evaluate to 1 because python evaluates these logical expressions left-to-right. a永远不会是 2。 (1 or 2)将始终计算为1 ,因为 python 从左到右计算这些逻辑表达式。

If the left-most one is not False, empty list, None, etc then it will assign that value to a and stop reading.如果最左边的不是 False、空列表、None 等,那么它将将该值分配给a并停止读取。 The interpreter only looks at the second value if the first one is "no good".如果第一个值“不好”,解释器只会查看第二个值。

is it possible for a variable of type int or float or string to hold multiple values at the same time? int、float 或 string 类型的变量是否可以同时保存多个值?

Maybe in Quantum Computing, but certainly not in normal programming.也许在量子计算中,但在普通编程中肯定不是。

You're misunderstanding what the posted syntax does.您误解了发布的语法的作用。 When you assign a value using that expression, it assigns the first "truthy" value it comes across in left-to-right order.当您使用该表达式分配一个值时,它会按从左到右的顺序分配它遇到的第一个“真实”值。 Any remaining value candidates after that first truthy one are discarded/ignored.在第一个真实值之后的任何剩余值候选者都将被丢弃/忽略。

As it stands, the example you gave is pretty redundant - non-empty strings are "truthy", and since it's a literal "b", the empty list never even gets considered.就目前而言,您给出的示例非常多余-非空字符串是“真实的”,并且由于它是文字“b”,因此甚至从未考虑过空列表。 That code is fundamentally equivalent to:该代码基本上等同于:

b = {
  'b': "b"
}

With respect to your code snippets, the or ed expressions are evaluated when they are assigned:对于您的代码片段, or ed 表达式在分配时进行评估:

Since x or y returns x if x evaluates to True , else returns y ,由于x or y如果x计算结果为True则返回x ,否则返回y

the values of b and a from the examples are:示例中ba的值是:

b = {'b': 'b'}
a = 1

and that is, what the print function returns.也就是说,打印 function 返回的内容。

Concerning the final question: It is not possible that a is 1 and 2 at the same time, but you can create a new type, so that a equals 1 and 2 at the same time:关于最后一个问题: a不可能同时1 和 2,但是您可以创建一个新类型,使a同时等于1 和 2:

class multiple_values:
  def __init__(self, data):
    self.data = set(data)
  def __eq__(self, value):
    return value in self.data

a = multiple_values([1, 2])

A list of 'valid' values is given to initialise a , but a itself is not a list.给出了一个“有效”值列表来初始化a ,但a本身不是一个列表。

a == 1 as well as a == 2 evaluate to True , while eg a == 3 evaluates to False . a == 1a == 2评估为True ,而例如a == 3评估为False

But please note, that the type definition for multiple_values would need to be expanded in order to be useful for more than just an equality test.但请注意, multiple_values的类型定义需要扩展,以便不仅仅用于相等测试。

Also, you asked for an int , float or string to have multiple values.此外,您要求intfloatstring具有多个值。 a is not an int - it only 'behaves' like one in this limited example. a不是一个int - 它只是在这个有限的例子中“表现”得像一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM