Re: A Single Instance of an Object?

Liste des GroupesRevenir à cl python 
Sujet : Re: A Single Instance of an Object?
De : rambiusparkisanius (at) *nospam* gmail.com (Ivan \"Rambius\" Ivanov)
Groupes : comp.lang.python
Date : 11. Mar 2024, 23:37:34
Autres entêtes
Message-ID : <mailman.89.1710193069.3452.python-list@python.org>
References : 1 2 3
On Mon, Mar 11, 2024 at 5:06 PM dn via Python-list
<python-list@python.org> wrote:
>
Good question Rambius!
>
On 12/03/24 09:53, Ivan "Rambius" Ivanov via Python-list wrote:
Hello,
>
I am refactoring some code and I would like to get rid of a global
variable. Here is the outline:
>
import subprocess
>
CACHE = {}
>
First thought: don't reinvent-the-wheel, use lru_cache
(https://docs.python.org/3/library/functools.html)
>
>
The global cache variable made unit testing of the lookup(key) method
clumsy, because I have to clean it after each unit test. I refactored
it as:
>
class Lookup:
     def __init__(self):
         self.cache = {}
>
>
Change "cache" to be a class-attribute (it is currently an instance.
>
Then, code AFTER the definition of Lookup can refer to Lookup.cache,
regardless of instantiation, and code within Lookup can refer to
self.cache as-is...
>

Thank you for your suggestions. I will research them!

Regards
rambius


--
Tangra Mega Rock: http://www.radiotangra.com

Date Sujet#  Auteur
11 Mar 24 o Re: A Single Instance of an Object?1Ivan \"Rambius\" Ivanov

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal