Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; 'sys': 0.07; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; '(module)': 0.16; 'a(object):': 0.16; 'dict': 0.16; 'globals.': 0.16; 'inconvenient': 0.16; 'subject:class': 0.16; 'there!': 0.16; 'to:addr:web.de': 0.16; 'undo': 0.16; '{})': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'hack': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; '>>>>': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'cases': 0.33; 'classes': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'so,': 0.37; 'name': 0.63; 'our': 0.64; 'become': 0.64; 'situation': 0.65; 'skip:m 50': 0.68; 'otten': 0.84; 'do:': 0.91; 'good,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=TdF1EuGOGYm+4rzcfvnUxkUfvKIxG8roZ/jLPWKsVMA=; b=r4+qaJCI8i/10QLxJX1F1tz0wtQ4e87ZCxAjqBvDL1XekAQo5hpjsrlOecv6yM2y0M xPaKfAm/0YQG/0tqnn/hwICTOpIjV4tJbkkx/sYI5Y2yDLgFzm6BmpRPoAbFFsT+95rI GBSnh7uEDB9QPVlZ1BU+S9jDrFc8Wj55CUf4oDZ13HGdZ0TiXvs0XHRQNBuqQBHgxdXU T9xFko/E846VpTe5rmw6iQ3lwA0OLsYC1ubuHYYb8dqQVH4lbM6Dsw4uIfD6IsO/Cen2 DylNuytMkpggH8AuLnqhzWI4k/vh3aTol+bDkqqnsgw6uDHlKfBuY4xovr1MQChCMqI/ d/KQ== X-Received: by 10.112.88.169 with SMTP id bh9mr2378603lbb.12.1372259865947; Wed, 26 Jun 2013 08:17:45 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <138ce5a8-1b4c-49d4-8005-d5f4642dcb97@googlegroups.com> <5d6aa501-58f6-4d8f-8bf5-53d95ff9264e@googlegroups.com> From: Joshua Landau Date: Wed, 26 Jun 2013 16:17:05 +0100 Subject: Re: class factory question To: Peter Otten <__peter__@web.de> Content-Type: text/plain; charset=UTF-8 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372259876 news.xs4all.nl 15948 [2001:888:2000:d::a6]:36836 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49264 On 26 June 2013 15:46, Peter Otten <__peter__@web.de> wrote: > The clean way to > cope with the situation is to use a dict: > > classnames = ["Vspace", ...] > classes = {name: type(name, ...) for name in classnames} > > Then you can access the Vspace class with > > classes["Vspace"] > > If that is inconvenient for your usecase you can alternatively update the > global (module) namespace: > > globals().update((name, type(name, ...) for name in classnames) > > For example: > >>>> class A(object): pass > ... >>>> globals().update((n, type(n, (A,), {})) for n in ["Beta", "Gamma"]) >>>> Beta > >>>> issubclass(Beta, A) > True I would say if a dict isn't good, there are still some cases where you might not want to use globals. I _might_ do: import sys from types import ModuleType # As before newclasses = ['Vspace', 'Boldpath', "and so on", "and yes, these all become variables"] little_classes = {name: type(name, (int,), {}) for name in newclasses} # Make a module module_for_little_classes = ModuleType("module_for_little_classes", "All the things") module_for_little_classes.__dict__.update(little_classes) # Hack it in there! sys.modules["module_for_little_classes"] = module_for_little_classes # Now we can undo all import module_for_little_classes as mlc mlc.Vspace mlc.Boldpath ... # And undo all our hard work avoiding globals(): from module_for_little_classes import * Vspace Boldpath So, why avoid globals()? 1) Linters don't like globals() 2) Urm... it's ugly? 3) Ur.......