Python Help

Can someone please tell me what the heck this means in Python?


I tried all sorts of searches on the internet and couldn’t find it under:

  • multi bracket associative
  • multi key list / dictionary
1 Like

I’m not up on my Python, but that looks like object notation to me (maybe just my Javascript influencing me though). Looks like “profiles” is an associative array (i.e. Map), with a key referenced by variable profileName. The object returned has a member called “CostMatrix”, which is an object with a member called “tpWeight”. In Java, this line might be written as (“self” implies static, so “this” is not quite right here, but you should get the idea):

this.profiles.get( profileName ).CostMatrix.tpWeight

Depending on how it was translated to Java, those two subproperties might not be instances of some class, but instead associative arrays themselves, in which case this could be written another way like:

this.profiles.get( profileName ).get( "CostMatrix" ).get( "tpWeight" );

Someone with more experience with Python will probably say I am completely wrong :wink:

1 Like

Looks like a dict of dicts.

self.profiles is a dict, and profileName is probably a string key for the dict. The value of that lookup is another dict with a key of CostMatrix, which has a value of another dict with a key of tpWeight.

1 Like

In Python, these are called “dictionaries” or _dict_s. In dicts, you cannot use object property notation ( to lookup a string key value. You must use an array-style element lookup like foo["bar"].

1 Like

Thanks Guys! You are all Gentlemen and Scholars! :wink:

1 Like

You’ll typically get a structure like that if you load in json ie/ self.profiles=json.loads("{raw:‘info’}")

It’s not a standard dict (associative array) and Python doesn’t do the multi-dimensional lookup out-of-the-box. But you can use this class definition if you’d like to create your own:

class NestedDict(dict):
    def __getitem__(self, key):
        if key in self: return self.get(key)
        return self.setdefault(key, NestedDict())


@jonincanada That makes sense, and it is in fact the case that that data structure arises out of reading in a JSON nested data structure. Thanks for your insight on that! :thumbsup: