Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

It's hard to tune heavily tuned code. :-) -- Larry Wall in <199801141725.JAA07555@wall.org>


devel / comp.lang.python / Re: Using __new__

SubjectAuthor
o Re: Using __new__MRAB

1
Re: Using __new__

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.samoylyk.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Using __new__
Date: Sat, 17 Feb 2024 23:08:57 +0000
Lines: 44
Message-ID: <mailman.71.1708211526.3227.python-list@python.org>
References: <CAApdmf1-8Mu4wK+V0Sd61v-ySZE0+pLO5OHUZOXa1aubty1LvA@mail.gmail.com>
<692bffe8-0b64-48a8-8c7b-fb343c830b35@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de FqKqFofdKRZ8oaPeSg5K7gv8ItOk0b4X0f0RZ81wtkEQ==
Cancel-Lock: sha1:OETIAoraaTRvR0WM/3zq7dC1Y40= sha256:Sjzs3jEQLEJ/p267OYz9/RAxSSV+7uDS877HY9tWyc8=
Return-Path: <python@mrabarnett.plus.com>
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=plus.com header.i=@plus.com header.b=cwkbR0+x;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'argument':
0.04; 'def': 0.04; 'traceback': 0.04; '(most': 0.05; 'last):':
0.05; 'from:addr:python': 0.09; 'received:192.168.1.64': 0.09;
'typeerror:': 0.09; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16;
'none:': 0.16; 'proceed?': 0.16; 'received:84.93': 0.16;
'received:84.93.230': 0.16; 'received:plus.net': 0.16; 'wrote:':
0.16; 'python': 0.16; '16,': 0.19; 'to:addr:python-list': 0.20;
'skip:_ 10': 0.22; 'code': 0.23; 'expect': 0.28; 'suggestions':
0.28; 'header:User-Agent:1': 0.30; 'takes': 0.31; "doesn't": 0.32;
'"",': 0.32; 'python-list': 0.32; 'subject:Using': 0.32;
'received:192.168.1': 0.32; 'header:In-Reply-To:1': 0.34;
'following': 0.35; 'class': 0.37; 'received:192.168': 0.37;
'file': 0.38; 'quite': 0.39; 'use': 0.39; 'method': 0.61; 'skip:o
10': 0.61; "there's": 0.61; 'ever': 0.63; 'pass': 0.64; 'skip:k
10': 0.64; 'exactly': 0.68; 'puzzled': 0.84; 'skip:" 50': 0.84;
'skip:j 20': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1708211338; bh=9Zu73V0E9xFkYl1WFc8GktNCEnIWgUQkuelwwMWWFLY=;
h=Date:Subject:To:References:From:In-Reply-To;
b=cwkbR0+xxJsExcNEOfuvBfcqfs6uk2G3hvJEPlP8jg8qKx92IAHDNBecbDgrmddJA
V8NIiIijO7thYo+bKCDl1b0PtoseRUi9KjtKUY1C0crP3qb7eXWFjmRFTuOFeNd8FX
Bhr/fcCpgZQydybT3NAaAgwvEY9GAebr9oTI5NzgIKEaes2EAWGU0vTZY+XwCIOAQv
dT72udsbJMs5jAYqwqMGls7zGtOj5d5c5xMWzYx744D2JaGXihhKSDuqfZyjsOotMz
oGw+1CzlVOOkvK2p7LahShRAQzVhMtzuLFEYbKLwwceTGrGW+SFF5VEWOMqqws+B3N
9rnh4JyHH2H+A==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=XP2TShhE c=1 sm=1 tr=0 ts=65d13c8a
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=D0l_vaF719cEaWylqWUA:9 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <CAApdmf1-8Mu4wK+V0Sd61v-ySZE0+pLO5OHUZOXa1aubty1LvA@mail.gmail.com>
X-CMAE-Envelope: MS4xfEbiebAM7BoWXsIp/n8G1mcWNSiN/E0pHvW9rV/KBGFuPLgVArYuZhiZXKjBMnL3mB794TYHE5Lm9W9uxU280OCDOnjzSrzCDG40sDQPxfRfYQuM8Iw0
QBoZAlRoGhKPtCSwVB3yve3dvMwggEvNjcWFVTNuTsOGBN561SH53iUGCccEWEKSDqFYbWM3Yk5BHXcHrzKFeA6WabyE9zM6nT0=
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: <692bffe8-0b64-48a8-8c7b-fb343c830b35@mrabarnett.plus.com>
X-Mailman-Original-References: <CAApdmf1-8Mu4wK+V0Sd61v-ySZE0+pLO5OHUZOXa1aubty1LvA@mail.gmail.com>
 by: MRAB - Sat, 17 Feb 2024 23:08 UTC

On 2024-02-17 22:35, Jonathan Gossage via Python-list wrote:
> I am attempting to use the __new__ method in the following code:
> class SingletonExample(object):
>
> _instance = None
>
> def __new__(cls, **kwargs):
> if cls._instance is None:
> cls._instance = super().__new__(cls, **kwargs)
> return cls._instance
>
> def __init__(self, **kwargs) -> None:
> our_attributes = ('h', 'x')
> if kwargs is not None:
> for k, v in kwargs.items():
> if k in our_attributes:
> setattr(self, k, v)
>
> a = SingletonExample(h=1)
>
> and I get the following result:
>
> (PRV) jonathan@jfgdev:/PR$ python -m Library.Testing.test2
> Traceback (most recent call last):
> File "<frozen runpy>", line 198, in _run_module_as_main
> File "<frozen runpy>", line 88, in _run_code
> File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 16, in
> <module>
> a = SingletonExample(h=1)
> ^^^^^^^^^^^^^^^^^^^^^
> File "/mnt/ProgrammingRenaissance/Library/Testing/test2.py", line 6, in
> __new__
> cls._instance = super().__new__(cls, **kwargs)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> TypeError: object.__new__() takes exactly one argument (the type to
> instantiate)
>
> I am quite puzzled as it looks as if this code will not work if the
> super-class is 'object'. Any suggestions on how to proceed?
>
Don't pass kwargs to object.__new__ because it doesn't expect it.

Incidentally, kwargs will never be None, and there's no point in giving
a return type for __init__ because it can only ever return None.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor