Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

She sells cshs by the cshore.


devel / comp.lang.python / Re: A Single Instance of an Object?

SubjectAuthor
o Re: A Single Instance of an Object?dn

1
Re: A Single Instance of an Object?

<mailman.87.1710190996.3452.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29644&group=comp.lang.python#29644

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonList@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: A Single Instance of an Object?
Date: Tue, 12 Mar 2024 10:03:01 +1300
Organization: DWM
Lines: 35
Message-ID: <mailman.87.1710190996.3452.python-list@python.org>
References: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
<1a49ecb0-2a5f-4758-92d1-a70d3d3dc651@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de hJW0HaKMnkzYwZvf/35GvAZTb0lg5YztNT93HM9V8aXQ==
Cancel-Lock: sha1:UFhLJCN0YsExJnJXsk+0j09UZvg= sha256:RqO8XuqeBdgmwxd+NKiZk8mCcnR3q5dIqZVAqnizPAU=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=I6zw5Qjq; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'variable':
0.05; '=dn': 0.09; 'from:addr:danceswithmice.info': 0.09;
'from:addr:pythonlist': 0.09; 'lookup': 0.09; 'import': 0.15;
'"cache"': 0.16; '(it': 0.16; 'message-id:@DancesWithMice.info':
0.16; 'outline:': 0.16; 'received:51.254': 0.16;
'received:51.254.211': 0.16; 'received:51.254.211.219': 0.16;
'received:cloud': 0.16; 'received:rangi.cloud': 0.16;
'refactoring': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'to:addr
:python-list': 0.20; 'skip:_ 10': 0.22; 'code': 0.23; 'header
:User-Agent:1': 0.30; 'header:Organization:1': 0.31; 'question':
0.32; 'python-list': 0.32; 'header:In-Reply-To:1': 0.34; 'change':
0.36; 'currently': 0.37; 'class': 0.37; 'received:192.168': 0.37;
'use': 0.39; 'hello,': 0.40; 'method': 0.61; 'here': 0.62;
'definition': 0.64; 'ivan': 0.64; 'received:51': 0.64; 'within':
0.69; 'global': 0.73; 'unit': 0.81; 'rid': 0.84
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud C315B4FD
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1710190988;
bh=ZFWIgATF4dlXC6U+VKnECWYRg9KQvzcTnlLoVY4fZhY=;
h=Date:From:Subject:To:References:In-Reply-To:From;
b=I6zw5Qjq+hFwAnjPvgpVc7Z5r35w3aZwXaiwlIg/bFbx5DkwgsGhF8jZlRxGaW69Q
BKUQXw2Izn9AkafIGy6LdeGy+cGVpQpQe/VzSY7OAgTUXVFf0u5U6cmKj48fg2mWct
H1c8wKHsxPJn0uVY+RONgsQFa2KtFui2amPN0vYPnuRCxCg2qy7uXRWCquN1V5k4p/
GybyRpw3XRrvY4iDxocskU//5gcHst2kcqGETVQ16Rn1k91HYXSRA/SqMhcdjJqgiY
7hhgid1i2Fa9jMjpFzmZYXDRtoaB772zDzHdjeoso04i6rL+BZ8syg218Ny3+w5+Dt
SxFq2PjEv28fA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <1a49ecb0-2a5f-4758-92d1-a70d3d3dc651@DancesWithMice.info>
X-Mailman-Original-References: <CAE9rwzOnEiBSf_m19EQByXQ_Qyk=GkiEzBUf=WMfaeZvR9102g@mail.gmail.com>
 by: dn - Mon, 11 Mar 2024 21:03 UTC

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...

--
Regards,
=dn

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor