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 8073123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:14:13+00:00 2026-06-05T14:14:13+00:00

I wanted, to make traversable (by DB, single file or just as string) class

  • 0

I wanted, to make traversable (by DB, single file or just as string) class in python. I Write this (shorted):

from json import JSONDecoder, JSONEncoder
def json_decode(object): return JSONDecoder().decode(object)
def json_encode(object): return JSONEncoder().encode(object)

class Storage:
__separator__ = 'ANY OF ANYS'
__keys__ = []
__vals__ = []
__slots__ = ('__keys__', '__vals__', '__separator__')

def __getattr__(self, key):
    try:
        return self.__vals__[self.__keys__.index(key)]
    except IndexError:
        raise AttributeError

def __setattr__(self, key, val):
    self.__keys__.append(key)
    self.__vals__.append(val)

def store(self):
    return (json_encode(self.__keys__) + self.__separator__ +
            json_encode(self.__vals__))

def restore(self, stored):
    stored = stored.split(self.__separator__)
    for (key, val) in zip(json_decode(stored[0]), json_decode(stored[1])):
        setattr(self, key, val)

And yea – that work, but… When i’m making more instances, all of them are like singleton.

So – how to set attribute to instance without _setattr_?

PS. I got idea – make in set/getattr an pass for keys/vals, but it’ll make mess.

  • 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-06-05T14:14:16+00:00Added an answer on June 5, 2026 at 2:14 pm

    your __separator__, __keys__, __vals__ and __slots__ are attributes of the object “Storage”(class object). I don’t know if it’s exactly the same, but I’d call it static variables of the class.

    If you want to have different values for each instance of Storage, define each of these variables in your __init__ function:

    class Storage(object):
        __slots__ = ('__keys__', '__vals__', '__separator__')
        def __init__(self):
            super(Storage, self).__setattr__('__separator__', "ANY OF ANYS")
            super(Storage, self).__setattr__('__keys__', [])
            super(Storage, self).__setattr__('__vals__', [])
    
        def __getattr__(self, key):
            try:
                vals = getattr(self, '__vals__')
                keys = getattr(self, '__keys__')
                return vals[keys.index(key)]
            except IndexError:
                raise AttributeError
    
        def __setattr__(self, key, val):
            vals = getattr(self, '__vals__')
            keys = getattr(self, '__keys__')
            vals.append(val)
            keys.append(key)
    

    edited so getattr and setattr works

    I got that problem 2 days ago. Don’t know if that’s exactly your problem, but you said that about “its like I have a singleton”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wanted to make sure: When I write: require('/path/to/file/file.php'); Is the file required with
I wanted to make a linked list class ListList that inherits from a class
I wanted to make some simple file recovery software, where I want to try
I wanted to make a post request in this website: http://www.prezup.info/index.php?page=films in order to
I wanted to make a option class, where you could load all preferences of
I wanted to make mysql dump files using a batch file and an app
I wanted to make a tryout this weekend with Cassandra + PHP in my
If I wanted to make an iPhone game like this one (ignoring the multiplayer
if wanted to make a this method print using zero pad how do you
I wanted to make a query with Rails, something like this: filters = Filter.joins(:category_filters).where(category_id

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.