Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4118796
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:00:26+00:00 2026-05-20T23:00:26+00:00

I would like to pass a storage container location to a class. Is there

  • 0

I would like to pass a storage container location to a class. Is there a better way to do it than by passing a dict and the key? Passing keys feels a little odd, though it seems to work.

class GuiWidget():
    def __init__(self, storageDict, storageDictKey):
        self.storageDict = storageDict
        self.storageDictKey = storageDictKey
    def inc(self):
        self.storageDict[self.storageDictKey] += 1  # in real problem store image

storage = {"k1":0, "k2":0}

w1 = GuiWidget(storage, "k1")
w2 = GuiWidget(storage, "k2")


# calls from gui thread
print storage
w1.inc()
print storage
w2.inc()
print storage

##{'k2': 0, 'k1': 0}
##{'k2': 0, 'k1': 1}
##{'k2': 1, 'k1': 1}

—edit—

Though I would do it a little more verbosely, I like Andrey’s way. It works with either dict or list. It also gave me the idea for this solution with a class, though the differences in use seem more cosmetical:

class GuiWidget(object):
    def __init__(self, connector):
        self.connector = connector
    def inc(self):
        self.connector.v = (self.connector.v + 1)

class Connector(object):
    def __init__(self, container, key):
        self.container = container
        self.key = key
    def get_f(self):
        return self.container[self.key]
    def set_f(self, z):
        self.container[self.key] = z
    v = property(fget=get_f, fset=set_f)

storage = {"k1":0, "k2":0}
storageList = [0]

w1 = GuiWidget(Connector(storage, "k1"))
w2 = GuiWidget(Connector(storage, "k2"))
w3 = GuiWidget(Connector(storageList, 0))


# calls from gui thread
print storage
w1.inc()
print storage
w2.inc()
print storage
w3.inc()
print storageList

##{'k2': 0, 'k1': 0}
##{'k2': 0, 'k1': 1}
##{'k2': 1, 'k1': 1}
##[4]

All this reminds me of passing a pointer to a struct in C.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:00:26+00:00Added an answer on May 20, 2026 at 11:00 pm

    I guess you want to eliminate the dependence of your class on storage access method. That’s my solution:

    class GuiWidget():
        def __init__(self, accessFunc):
            self.accessFunc = accessFunc
        def inc(self):
            self.accessFunc(self.accessFunc() + 1)
    
    storage = {"k1":0, "k2":0}
    
    def getAccessFunc(key):
        return lambda v = None: v is not None and storage.update({key: v}) or storage[key]
    
    w1 = GuiWidget(getAccessFunc("k1"))
    w2 = GuiWidget(getAccessFunc("k2"))
    
    
    # calls from gui thread
    print storage
    w1.inc()
    print storage
    w2.inc()
    print storage  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.