0

I have several classes which have dictionaries as attributes. These dictionaries store instances of other classes. The lower/nested classes also have dictionaries as attributes as well.

Currently I am writing full loops, with breaks, to retrieve the nested values. Problem is that I need to do several processes/filters on the nested dictionaries so it seems computationally expensive as well as it leading to ugly, spaghetti-like code. What I would like to do is be able to access the nested items, update the items (in-place), and continue on.. Is there a way to do this?

Example code (let's assume you might wear several socks in each shoe and have several colored toes):

class Shoe: def __init__(self): self.socks = {} # this contains instances of socks class Sock: def __init__(self): self.toes = {} # this contains instances of toes self.color = 'green' class Toe: def __init__(self): self.color = 'blue' 

Now I want to look inside the Shoe instance and retrieve/update all Sock instances which are green and have blue Toes. Then update the Sock/Toe instances I retrieved so that when I access the shoe later the values have changed.

All help is appreciated!

4
  • Would a database structure work for you? Because this is one of the main reasons databases exist. Commented Jan 22, 2021 at 18:24
  • I don't think so - but I could very likely be wrong. This is for processing Computer Vision/Deep Learning tasks. Once the task is processed then I convert all this to JSON and send it over to the database. Isn't there latency for making db calls? (can't have latency). Commented Jan 22, 2021 at 18:26
  • 1
    I guess I should say, the latency of calling the db, and reconstructing the objects, needs to be less than the current nested loops to make it worthwhile. Commented Jan 22, 2021 at 18:30
  • How many attributes such as color do you have? If it is a single one, you might want to build a dictionary of color: instances. But if there are many this solution might be too slow. About the db, if your data is relatively small (say a few thousand entries), maintaining an in memory database would not induce (much) latency. (see docs for an example) Commented Jan 22, 2021 at 18:34

1 Answer 1

1

I suggest that you can use a list instead of obj

class Shoe: def __init__(self): self.socks = [] # this contains instances of socks class Sock: def __init__(self): self.toes = [] # this contains instances of toes self.color = 'green' class Toe: def __init__(self): self.color = 'blue' toe = Toe() socks = Sock() socks.toes.append(toe) shoes = Shoe() shoes.socks.append(socks) 

then you can iterate as you want

for s in shoes.socks: # s becomes in a list of sock for t in s.toes: # t becomes in a list of toes for color in t: print(color) 

or maybe i do not understand what you really need.

greeting

Sign up to request clarification or add additional context in comments.

2 Comments

Dang. Right after I posted this question I thought of using lists then I left the post up in hopes that that was not the answer. I think you are right. I am going to leave it open for about an hour to see if anyone else has a solution I am not thinking of. Then mark yours as the answer.
@DavidAlford how is this different than what you'd do with the dicts?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.