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

  • SEARCH
  • Home
  • 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 8725991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:09:45+00:00 2026-06-13T08:09:45+00:00

I tried a bit of code but it seems to cause issues: class Page:

  • 0

I tried a bit of code but it seems to cause issues:

class Page:
    cache = []


    """ Return cached object """
    def __getCache(self, title):
        for o in Page.cache:
            if o.__searchTerm == title or o.title == title:
                return o
        return None


    """ Initilize the class and start processing """
    def __init__(self, title, api=None):
        o = self.__getCache(title)
        if o:
            self = o
            return
        Page.cache.append(self)

        # Other init code
        self.__searchTerm = title
        self.title = self.someFunction(title)

Then I try:

a = Page('test')
b = Page('test')

print a.title # works
print b.title # AttributeError: Page instance has no attribute 'title'

Whats wrong with this bit of code? Why wont it work? Is there a way to make it work? If not how do I go about easily and transparently to the end user caching objects?

  • 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-13T08:09:46+00:00Added an answer on June 13, 2026 at 8:09 am

    if you want to manipulate the creation, you need to change __new__.

    >>> class Page(object):
    ...     cache = []
    ...     """ Return cached object """
    ...     @classmethod
    ...     def __getCache(cls, title):
    ...         for o in Page.cache:
    ...             if o.__searchTerm == title or o.title == title:
    ...                 return o
    ...         return None
    ...     """ Initilize the class and start processing """
    ...     def __new__(cls, title, api=None):
    ...         o = cls.__getCache(title)
    ...         if o:
    ...             return o
    ...         page = super(Page, cls).__new__(cls)
    ...         cls.cache.append(page)
    ...         page.title = title
    ...         page.api = api
    ...         page.__searchTerm = title
    ...         # ...etc
    ...         return page
    ... 
    >>> a = Page('test')
    >>> b = Page('test')
    >>> 
    >>> print a.title # works
    test
    >>> print b.title
    test
    >>> 
    >>> assert a is b
    >>> 
    

    EDIT: using __init__:

    >>> class Page(object):
    ...     cache = []
    ...     @classmethod
    ...     def __getCache(cls, title):
    ...         """ Return cached object """
    ...         for o in Page.cache:
    ...             if o.__searchTerm == title or o.title == title:
    ...                 return o
    ...         return None
    ...     def __new__(cls, title, *args, **kwargs):
    ...         """ Initilize the class and start processing """
    ...         existing = cls.__getCache(title)
    ...         if existing:
    ...             return existing
    ...         page = super(Page, cls).__new__(cls)
    ...         return page
    ...     def __init__(self, title, api=None):
    ...         if self in self.cache:
    ...             return
    ...         self.cache.append(self)
    ...         self.title = title
    ...         self.api = api
    ...         self.__searchTerm = title
    ...         # ...etc
    ... 
    >>> 
    >>> a = Page('test')
    >>> b = Page('test')
    >>> 
    >>> print a.title # works
    test
    >>> print b.title
    test
    >>> assert a is b
    >>> assert a.cache is Page.cache
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I tried running the following code product([1,2,3],['a','b']) it returned an object of the
I've done some looking around, but I'm still confused a bit. I tried Crockford's
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Code-wise this seems so simple, but I still get stuck on a #VALUE. I
Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools
I was a bit surprised a few minutes ago when I tried to overload
For some reason my 64 bit EPD can't import wx. I also tried to
Okay, this is starting to drive me a little bit nuts. I've tried several
Tried searching the site, but cannot find an answer to my problem: Lets say
I have a bit of an odd use-case for a Ruby Enumerable, it seems.

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.