Assigning empty value or string in Python
I would like to understand if there is a difference between assigning an empty value and an empty output, as follows:
1> Assigning a value like this
string = ""
2> An empty value returned as output
string = "abcd:"
str1, str2 = split(':')
In other words, is there a difference in values of 'string' in 1> and 'str2' in 2>? And how would a method see the value of 'str2' if it is passed as an argument?
Checking equality with ==
>>> string = ""
>>> s = "abcd:"
>>> str1, str2 = s.split(':')
>>> str1
'abcd'
>>> str2
''
>>> str2 == string
True
Maybe you were trying to compare with is
. This is for testing identity: a is b
is equivalent to id(a) == id(b)
.
Or check both strings for emptiness:
>>> not str2
True
>>> not string
True
>>>
So that both are empty ...
If you will check id(string)
in case-1 and id(str2)
in case2, it will give u the same value, both the string objects are same.
def mine(str1, str2):
print str1, str2
see the above method you can call mine(* string.split(':'))
it will pass the 'abcd:'
as str1 = 'abcd' and str2 = ''.
In other words, is there a difference in values of 'string' in 1> and 'str2' in 2>?
No, there is no difference, both are empty strings ""
.
And how would a method see the value of 'str2' if it is passed as an argument?
The method would see it as a string of length 0, in other words, an empty string.
链接地址: http://www.djcxy.com/p/19588.html上一篇: 用jQuery.ajax发送multipart / formdata
下一篇: 在Python中分配空值或字符串