Deutsch   English   Français   Italiano  
<mailman.65.1747692692.3008.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: Thomas Passin <list1@tompassin.net>
Newsgroups: comp.lang.python
Subject: Re: Dynamic classes
Date: Mon, 19 May 2025 18:11:20 -0400
Lines: 73
Message-ID: <mailman.65.1747692692.3008.python-list@python.org>
References: <CAApdmf3UwA6zf2-eSfd=1U=Unx3-6PUj6+XS0Sp62rkn73C8iQ@mail.gmail.com>
 <385fb601-4061-4785-809a-94ee1e6c4e11@wichmann.us>
 <1139f617-1f4e-4ccc-bbfe-bd8bf0ef02e3@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de /EAGsg+REHz2ATz+olUVdgyDkp6MC/HbGuVzW2bG8abg==
Cancel-Lock: sha1:r2EG5XAPYCU7yFX7HKhVSZuA6Ow= sha256:2OaYIM5F1JKExWSft+uly86ZTSn3uwxJafZb0Ar+qUs=
Return-Path: <list1@tompassin.net>
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=tompassin.net header.i=@tompassin.net header.b=NUV6pFzj;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'class,':
 0.05; 'class.': 0.07; 'fails': 0.09; '5:49': 0.16; 'account)':
 0.16; 'init': 0.16; 'instance': 0.16; 'jython': 0.16; 'mats':
 0.16; 'received:10.0.0': 0.16; 'received:100.112': 0.16;
 'received:64.90': 0.16; 'received:64.90.62': 0.16;
 'received:64.90.62.162': 0.16; 'received:dreamhost.com': 0.16;
 'unpacking': 0.16; 'variables,': 0.16; 'wichmann': 0.16; 'wrote:':
 0.16; 'pm,': 0.19; 'to:addr:python-list': 0.20; "i've": 0.22;
 'skip:_ 10': 0.22; 'done': 0.28; 'error': 0.29; 'header:User-
 Agent:1': 0.30; 'blog': 0.30; 'present': 0.30; 'module': 0.31;
 'received:100': 0.31; '"",': 0.32; 'python-list': 0.32;
 'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
 'received:relay.mailchannels.net': 0.32; 'subject:Dynamic': 0.32;
 'but': 0.32; 'there': 0.33; 'handling': 0.35; 'header:In-Reply-
 To:1': 0.35; 'following': 0.35; "it's": 0.36; 'using': 0.37;
 'class': 0.37; 'way': 0.38; 'something': 0.39; 'try': 0.40;
 'similar': 0.63; 'skip:m 20': 0.63; 'rate': 0.67; 'accept': 0.67;
 'header:Received:6': 0.67; 'received:64': 0.67; 'clarity': 0.69;
 'interesting': 0.71; 'name,': 0.75; 'skip:f 20': 0.75;
 'coordinator': 0.80; 'btw': 0.84; 'factory': 0.84; 'stating':
 0.84; 'ancient': 0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1747692682; a=rsa-sha256;
 cv=none;
 b=YCUcZqm0JnVZONY1GS5tL0PLmaEBbiaVUIAlELkfFQsv3h+TsNFiddEu2yBisBa20YyaGE
 UsyYjiD8rGRXN4E97nOColCd+vhNcx/92s1pSCsf90rZ0HbUhvvkKG8/1N/LkOzEmuQZ4P
 qyA6VvMOVQJ+jtnGKunqP850ziJTZ2Q9rEKmTmJIzPUhV48lDRC1/exuHxs+arpXKPeWiL
 KiSsuj6zk5yKyYB5pizFmtGToihOL+NnXKTGlX4zq5xTlHWO58225g2ypA60CHsnUYnKzw
 qy15qIytQT1ldGPabif/tjcsXKIdnBQRihgtashpXltiZjmAYIKWPeByXmUTUg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
 d=mailchannels.net; s=arc-2022; t=1747692682;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:content-type:content-type:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references:dkim-signature;
 bh=L79fRs7dUvh/8kg3WDBZzBlO94yn1V7WWigb69LKvIc=;
 b=D3Fre2WK/DiwiQqK7KqHHrRaiqmb+WjRd3kN6wdP7VsTZnNakjDavW6PvDFskz1xTOfxBz
 iFhLPJLFDli8WN2zqKZ/VGRUrv3/ZccrQnobZXAYGdCidst5mop+WbN3OPHzM4ulwFxg4w
 AYIw5eb3T3hHXJgG08nG+96XcLBoF6gZUNO6YqysX9+GBb9PijUHiT+hjgq9BcURC/TGAq
 Z2R8iEU0oJB7wnT5zZGTzHv06rk8moksBA1YRCGTxECNJk9+qyykC/XFcjYFM0cRoUpqed
 +qusjwmrql/o/alq+VxpknaJ+GQjbLEfgyE+LylirHEuY+dPYDGLqTpK25oIUg==
ARC-Authentication-Results: i=1; rspamd-5b49d78d5-t2p62;
 auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Shrill-Wide-Eyed: 59b6fc0c7b1fdff2_1747692682735_2962006662
X-MC-Loop-Signature: 1747692682735:2835971716
X-MC-Ingress-Time: 1747692682734
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
 s=dreamhost; t=1747692682;
 bh=L79fRs7dUvh/8kg3WDBZzBlO94yn1V7WWigb69LKvIc=;
 h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
 b=NUV6pFzjZ6VgGoufL5WHt7c8wBCkGXg5BjM8fKnv+2yHRW/1c5e0J4kM8gqrxt3T7
 iyhdZTZ8VPaUhu2pWfe/+p6q/xPRm6qwuWQITAUHLpwQ1DiTeBZP80BJHaD9gq0Egh
 TtnmbXCl5Sht1nu+Ujsa4QUH/zhhWCIiitHA9MA3zKGwCthT+gUJ1PAaBgmb74E6PL
 RwNcaWy7ho5AnTTgZkwoXucKzOTxaZoK2UlGI540Wfv1NzrTG6g2WXFuTF1W/CPOKm
 lvaT3uzu3okkjXRtx2ZvlN6MDqsZ5S2DPE2dXHOo3MR9vYrQUQHfgJYJq0WJm+3rw9
 WiSvQmEsegf/Q==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <385fb601-4061-4785-809a-94ee1e6c4e11@wichmann.us>
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: <1139f617-1f4e-4ccc-bbfe-bd8bf0ef02e3@tompassin.net>
X-Mailman-Original-References: <CAApdmf3UwA6zf2-eSfd=1U=Unx3-6PUj6+XS0Sp62rkn73C8iQ@mail.gmail.com>
 <385fb601-4061-4785-809a-94ee1e6c4e11@wichmann.us>
Bytes: 8167

On 5/19/2025 5:49 PM, Mats Wichmann via Python-list wrote:
> On 5/19/25 09:51, Jonathan Gossage via Python-list wrote:
>> I have created a dynamic class using the type() function:
>> x = type('MyFlags', (), {'Flag1': 1, 'Flag2': 2, 'Flag3: 4, ' 
>> '__init__' :
>> __init__})
>> The new class is there, and the class variables, Flag1, Flag2, and Flag3,
>> are present correctly. However, when I try to create an instance of this
>> class with the following code:
>> y = x('Flag1', 'Flag2')
>> it fails with a TypeError stating that 'MyFlags' does not accept 
>> arguments.
>> What do I have to do to make this happen?. BTW __init__(self, *args) is
>> defined as the instance initializer.
> 
> Might help if you show the init function. I've done something similar to 
> this without trouble, but not using the unpacking (i.e. *args). I used 
> this in an ancient blog post (thus, pre-typing, and such):
> 
> def transact(acct, amount):
>      acct.balance += amount
> 
> def pay_interest(acct):
>      acct.balance += acct.balance * acct.interest_rate
> 
> def account_init(acct, num, name, bal, rate):
>      acct.acct_number = num
>      acct.acct_holder = name
>      acct.balance = bal
>      acct.interest_rate = rate
> 
> account = {
>      "acct_number": "XXX",
>      "acct_holder": "",
>      "balance": 0.0,
>      "interest_rate": 0.0,
>      "transact": transact,
>      "pay_interest": pay_interest,
>      "__init__": account_init,
> }
> 
> AccountType = type("AccountType", (), account)
> 
> myaccount = AccountType("1234567", "J. Q. Public", 20.0, 0.01)
> print(myaccount.balance)
> myaccount.transact(-10)
> print(myaccount.balance)
> myaccount.pay_interest()
> print(myaccount.balance)
> 

It's interesting that in Jython there is a way to do something 
conceptually similar to turn a Jython class into a Java class.  Here's 
one of mine:

synchronized CoordinatorType getCoord() {
     JythonObjectFactory factory = new JythonObjectFactory (
         // Type class, Jython module name, class name
         CoordinatorType.class, "Coordinator", "Coordinator");

     // Java class
     CoordinatorType coord = (CoordinatorType) factory.createObject();
     return coord;
}

// Instantiate a Coordinator
// Error handling elided for clarity
CoordinatorType c;
c = getCoord();