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

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

Path: ...!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: Left Right <olegsivokon@gmail.com>
Newsgroups: comp.lang.python
Subject: Re: Dynamic classes
Date: Tue, 20 May 2025 08:03:29 +0200
Lines: 28
Message-ID: <mailman.67.1747767982.3008.python-list@python.org>
References: <CAApdmf3UwA6zf2-eSfd=1U=Unx3-6PUj6+XS0Sp62rkn73C8iQ@mail.gmail.com>
 <CAJQBtgmOKat7eVWcVzatLwvU+Xy_Ntj9BeRf1tBb-QEHxTBFSw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de m4eiz7OJJgherYwDgKSKBAI5kVngl/Qtm6qJOK0yqyNA==
Cancel-Lock: sha1:xLdyiC44DxOmC5JWSEuMGfJEFIA= sha256:ljfP7B8Ax9Sw0F0kGdoieDLSBANaLwBm7ziJgoOB1Qs=
Return-Path: <olegsivokon@gmail.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=gmail.com header.i=@gmail.com header.b=huCoXh2f;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.014
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'bunch': 0.05;
 'class,': 0.05; 'cc:addr:python-list': 0.09; 'etc.).': 0.09;
 'methods,': 0.09; 'cc:no real name:2**0': 0.14; 'accomplishes':
 0.16; 'arguments': 0.16; 'mechanisms': 0.16; 'python': 0.16;
 'cc:addr:python.org': 0.20; 'language': 0.21; 'creates': 0.22;
 'skip:_ 10': 0.22; 'goal': 0.23; 'cc:2**0': 0.25; 'environment':
 0.29; 'approach': 0.31; 'message-id:@mail.gmail.com': 0.31;
 'accomplish': 0.32; 'end.': 0.32; 'format,': 0.32;
 'subject:Dynamic': 0.32; 'but': 0.32; 'received:google.com': 0.34;
 'same': 0.34; 'from:addr:gmail.com': 0.35; 'header:In-Reply-To:1':
 0.35; 'also,': 0.36; "it's": 0.36; 'using': 0.37; 'class': 0.37;
 'use': 0.39; '(with': 0.39; 'methods': 0.39; 'define': 0.40;
 'difficult': 0.40; 'method': 0.61; 'benefit': 0.63; 'similar':
 0.63; 'generally': 0.67; 'ability': 0.71; 'bodies,': 0.84;
 'factory': 0.84; 'inherit': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1747721032; x=1748325832; darn=python.org;
 h=cc:to:subject:message-id:date:from:in-reply-to:references
 :mime-version:from:to:cc:subject:date:message-id:reply-to;
 bh=S5RhRbfUlm0/1mYjBTuo3kGfCBnjfdm4M/LDTqJC+5A=;
 b=huCoXh2f006tUSGFah9CRHQta/jCUs+/htEIo+LDf6KWvdIdlJ7EvpdE8tNOMTMuEB
 wwOsSarl/OhOeJJngwEGmLcJIRssSJ63ZfMN+uJ+M8Qv04R7KtLP87XYfnbf0kxZU4PY
 NNoNpoCRm9mAxKgJZY0M1S6ie39Z8rrIVPqSIk+a4wGqrZQPgXG42FtcZpeWCzv1jXo5
 vWK8TGrr3hSOQftCFf127PD75DaBcsXpJMo82TdvBA+GzY9zG+NGBwIxAhycWD6wvObe
 hPxts1YX/dOTEa1ItuPnFddwz3tOYSvkQId5BXXwO1pCa3oX24IV8nhDNH9lGvlhaAmg
 ZHkg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1747721032; x=1748325832;
 h=cc:to:subject:message-id:date:from:in-reply-to:references
 :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
 :reply-to;
 bh=S5RhRbfUlm0/1mYjBTuo3kGfCBnjfdm4M/LDTqJC+5A=;
 b=FQnBg8FLkCIHSAT3sOpDn7Du6NC3Xv9T4jek9vgfB5iUwYsNw2TjcHMXr7Z0d9zEcP
 STLEZ+zAFb0F4AKKYLiIAzX5rY9+MlzDQCmLjdKJ2/ee6G46NDqgQXP+HMQXzV4oBnKu
 fo+zRMoNnRp4jQpv5DWhMuIjt4YOxpeu5oPiKr1Jl6/Rdoibl44YBb9gg32o5smDmYXB
 KmgBdu3/p67px3EigyMsUE/Mg7fl3PT2WM4e+CD/0KFoJKk1cc9T0SNJj+8XHtNFd7kN
 eu/aIg5QKWc0R+oifpQ8d9xvhKlmrIG4C13fDQMfmV9Zp3UcwTQLbwf4+YjwNPnMPYP2
 AIGA==
X-Gm-Message-State: AOJu0YzztzpmFlSxnd59B12LbktrWQcU2mBNBJZ36LgM8nacHftNK6+I
 a8iUdZlr2SNskfjwZpsBbHn4u5xEDf9+iBM7XgiSOjrdDNMvQ9PlSekU5VnaBIumBsxECwiW9NK
 szl15xFkmC4P3MFmn43hsbrjYQ8ShSPV1Pw==
X-Gm-Gg: ASbGncsEa3AK3baOXFNDa+6pKV3Hqa4NfdluqRlB9m9Xbx8PXyi1SDykw1QsyADgljw
 utqMiFzAXcscM9r+t7GHYATCqXN0w5W7vK7qcx4NAZ/AS3XtL/auRAl+t8pdqdpJkcQ01AhnmvW
 pTgjuDXb9o7/bF0U348IXbpJuLX8V08JWagrPRqref/vekPFh/rSOjzhGZf7tDKwE=
X-Google-Smtp-Source: AGHT+IHyUXlqFP9Ft+bg0ko9PWS7aiXKULaNDNdcC1/o2My0mHIYgww7mycuSzIzBXO/uchB9Eaobnl0TRMIAtzv0so=
X-Received: by 2002:ac8:1289:0:b0:497:b511:95af with SMTP id
 d75a77b69052e-497b5119e6fmr115764171cf.45.1747721020979; Mon, 19 May 2025
 23:03:40 -0700 (PDT)
In-Reply-To: <CAApdmf3UwA6zf2-eSfd=1U=Unx3-6PUj6+XS0Sp62rkn73C8iQ@mail.gmail.com>
X-Gm-Features: AX0GCFvHfk3NiNswcbgTZUmshFYOvE8TGvBGFLIiFT5v3Is0pI7OBcSRqnwg2LY
X-Mailman-Approved-At: Tue, 20 May 2025 15:06:21 -0400
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: <CAJQBtgmOKat7eVWcVzatLwvU+Xy_Ntj9BeRf1tBb-QEHxTBFSw@mail.gmail.com>
X-Mailman-Original-References: <CAApdmf3UwA6zf2-eSfd=1U=Unx3-6PUj6+XS0Sp62rkn73C8iQ@mail.gmail.com>
Bytes: 6409

> I have created a dynamic class using the type() function:
> x = type('MyFlags', (), {'Flag1': 1, 'Flag2': 2, 'Flag3: 4, ' '__init__' :
> __init__})

I find that it's generally more convenient to do this using similar code:

def constructor(flag1, flag2):
    class _Hidden:
        def __init__(self):
            self.flag1 = flag1
            self.flag2 = flag2
    return _Hidden()

h = constructor('Flag1', 'Flag2')

This accomplishes the same goal (with some overhead, perhaps), but is
easier to format, the editor will recognize that you are writing a
class rather than a bunch of data bits, you will have the ability to
define the methods together with the class, benefit from the class
initialization environment (eg. by using @static or @property
decorators etc.). Also, this allows class parametrization in ways that
are difficult to accomplish using metaclasses and other complicated
mechanisms Python language provides to that end. Eg. you can
conditionally inherit from different superclasses (so, you can use
this approach as a factory that creates different classes), or you can
conditionally add methods, conditionally provide different method
bodies, conditionally provide different arguments to parametrized
decorators.