square brackets after a function call

前端 未结 3 823
执念已碎
执念已碎 2020-12-12 00:48

I am a total novice at Python, and have come across a piece of code that confuses me.

ts, pkt2 = capPort2.wait(1, 45)[0]

The previous line

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 01:16

    Ah, I think this question answered my own recently, but I would like to extend the answer some:

    This call:

    var value = getUrlVars()["logout_url"];
    

    would end up setting the variable to the value of the 'logout_url' name-value pair that is returned from the function call to 'getUrlVars()', right? So you don't have to use just a numeric index, it can be used on hash/associative arrays/dictionary/etc results from the function.

    So if this is the function 'getUrlVars':

    function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace (/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
                vars[key] = value;
            }
        );
        return vars;
    }
    

    Which is returning key value pairs (from an input URL of ""http://a.place.com/page.html?name=fred&place=b3&logout_url=some.thing.net/go/here/file.html"), e.g.:

    'name'='fred',
    'place'='b3',
    'logout_url'='some.thing.net/go/here/file.html'         <-- URL encoded, most likely
    

    So my function call above would return "some.thing.net/go/here/file.html", while one that looks like this:

    getUrlVars()["name"]
    

    would return:

    "fred"
    

    I think. :)

    -- C

提交回复
热议问题