python function call syntax ... result = foo() ['abc']
number = droid.readPhoneState()['result']['incomingNumber']
What are 'result' and 'incomingNumber' in this syntax -- are they not parameters?开发者_如何学JAVA
How are they related to the function readPhoneState
?
import android
droid = android.Android()
droid.startTrackingPhoneState()
number = droid.readPhoneState()['result']['incomingNumber']
if number != None:
droid.speak('Call from '+str(number))
else:
droid.makeToast('No incoming call')
droid.readPhoneState()
returns a dict of dicts. Equivalent code:
outerDict = droid.readPhoneState()
innerDict = outerDict['result']
number = innerDict['incomingNumber']
result
and incomingNumber
are keys to a dictionary or an instance of a class that implements method __getitem__
. This means that readPhoneState()
returns a dictionary object, which supposed to have a key result
and the corresponding value is a dictionary object which supposed to have a key incomingNumber
.
the interpretation is that droid.readPhoneState()
returns a dict
, whose value corresponding to the key 'result'
is another dict
.
readPhoneState() is the method and it returns a dictionary object.
The dictionary object contains the property result
which is also a dictionary object containing the property incomingNumber
Supposedly, readPhoneState() returns a dictionary where values are, again, dictionaries.
With this syntax, you get the dictionary - returned by readPhoneState() - associated with key 'result' and ask it for the value whose key is 'incomingNumber'.
精彩评论