简体   繁体   中英

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'} . It got me questioning when will b ever be [] ? Or is it saying b is both "b" and [] ?

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 ?

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? ie, a is both 1 and 2 provided that it is not a list or dictionary?

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.

A string only evaluates false if it's empty. Your string "b" is not empty, so it will never be false. Thus there's no way for that expression "b" or [] to equal [] , it will always be "b" .

Not, it is not possible.

What you have done is assign to a the value of the expression (1 or 2) ; that is, the result of or -ing 1 and 2.

a will never be 2. (1 or 2) will always evaluate to 1 because python evaluates these logical expressions left-to-right.

If the left-most one is not False, empty list, None, etc then it will assign that value to a and stop reading. 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?

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. 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:

Since x or y returns x if x evaluates to True , else returns y ,

the values of b and a from the examples are:

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

and that is, what the print function returns.

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:

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 == 1 as well as a == 2 evaluate to True , while eg a == 3 evaluates to 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.

Also, you asked for an int , float or string to have multiple values. a is not an int - it only 'behaves' like one in this limited example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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