FAQ
I try to use "new.new.classobj (name, baseclass, dict)" and have no clue
what the "dict" of the current name space is. I can name dicts of
imported modules, because their name exists in the current name space.
If, for instance, I import a module "service" then that module's name
space would be "service.__dict__". But if I import * from service, then
I incorporate that name space into the current one and I cannot name it,
because the current module's name is not part of the module's own name
space. "dir (service)" is equivalent to "service.__dict__.keys ()" if
service is importet. "dir ()" is equivalent to "?.__dict__.keys ()"
where "?" is the name of the current module, itself not part of the
current module's name space. So the question mark stands for an implicit
name that can be neither named nor dropped. So my question is: how does
one name the dictionary of the name space one is in?

Frederic

Search Discussions

  • Thomas Jollans at Jul 5, 2010 at 9:27 am
    On 07/05/2010 11:07 AM, Anthra Norell wrote:
    I try to use "new.new.classobj (name, baseclass, dict)" and have no clue
    what the "dict" of the current name space is. I can name dicts of
    imported modules, because their name exists in the current name space.
    If, for instance, I import a module "service" then that module's name
    space would be "service.__dict__". But if I import * from service, then
    I incorporate that name space into the current one and I cannot name it,
    because the current module's name is not part of the module's own name
    space. "dir (service)" is equivalent to "service.__dict__.keys ()" if
    service is importet. "dir ()" is equivalent to "?.__dict__.keys ()"
    where "?" is the name of the current module, itself not part of the
    current module's name space. So the question mark stands for an implicit
    name that can be neither named nor dropped. So my question is: how does
    one name the dictionary of the name space one is in?
    either globals() or locals(), depending on what you mean.
    Frederic
  • Anthra Norell at Jul 5, 2010 at 12:16 pm

    Thomas Jollans wrote:
    On 07/05/2010 11:07 AM, Anthra Norell wrote:

    I try to use "new.new.classobj (name, baseclass, dict)" and have no clue
    what the "dict" of the current name space is. I can name dicts of
    imported modules, because their name exists in the current name space.
    If, for instance, I import a module "service" then that module's name
    space would be "service.__dict__". But if I import * from service, then
    I incorporate that name space into the current one and I cannot name it,
    because the current module's name is not part of the module's own name
    space. "dir (service)" is equivalent to "service.__dict__.keys ()" if
    service is importet. "dir ()" is equivalent to "?.__dict__.keys ()"
    where "?" is the name of the current module, itself not part of the
    current module's name space. So the question mark stands for an implicit
    name that can be neither named nor dropped. So my question is: how does
    one name the dictionary of the name space one is in?
    either globals() or locals(), depending on what you mean.

    Frederic
    Thomas,
    Thanks a million. Just the tip I needed.
    Frederic
  • Gregory Ewing at Jul 5, 2010 at 11:59 pm

    On 07/05/2010 11:07 AM, Anthra Norell wrote:
    I try to use "new.new.classobj (name, baseclass, dict)" and have no clue
    what the "dict" of the current name space is.
    Are you sure that's what you really want to know? The
    'dict' argument to classobj() defines the attributes
    that you want the new class to have. It's not meant
    to be the namespace in which the code creating the
    class is executing.

    --
    Greg
  • Anthra Norell at Jul 6, 2010 at 5:15 pm

    Gregory Ewing wrote:
    On 07/05/2010 11:07 AM, Anthra Norell wrote:

    I try to use "new.new.classobj (name, baseclass, dict)" and have no
    clue
    what the "dict" of the current name space is.
    Are you sure that's what you really want to know? The
    'dict' argument to classobj() defines the attributes
    that you want the new class to have. It's not meant
    to be the namespace in which the code creating the
    class is executing.
    No indeed I'm not sure. The doc explains the argument "dict" as "name
    space", a term I associated with the enclosing module's name space,
    because it is also visible from inside enclosed blocks.
    But how right you are! Passing locals () works fine inasmuch as
    the constructor doesn't complain. Looking subsequently at the class
    attributes with dir (c) or c.__dict__keys (), however, dumps the entire
    inventory of the module in addition to the attributes of the base class.
    Clearly, that can't be right.
    So, thanks to you! I very much appreciate the guidance along the
    right path.

    Frederic
  • Chris Rebert at Jul 5, 2010 at 10:03 am

    On Mon, Jul 5, 2010 at 2:07 AM, Anthra Norell wrote:
    I try to use "new.new.classobj (name, baseclass, dict)" and have no clue
    Slight tangent:
    Note that both the `new` module and old-style classes (which are what
    `classobj` produces) are deprecated.
    To produce new-style classes dynamically, use `type`.

    Cheers,
    Chris
  • Anthra Norell at Jul 5, 2010 at 1:26 pm

    Chris Rebert wrote:
    On Mon, Jul 5, 2010 at 2:07 AM, Anthra Norell wrote:

    I try to use "new.new.classobj (name, baseclass, dict)" and have no clue
    Slight tangent:
    Note that both the `new` module and old-style classes (which are what
    `classobj` produces) are deprecated.
    To produce new-style classes dynamically, use `type`.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris,
    I noticed the deprecation situation reading the doc, but opted for
    what I thought might be more backward-compatible. Your suggestion
    prompted me to take a closer look and it turns out that "types" is
    compatible as far back as I have to go (2.5). So I use "types" with
    thanks to you.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 5, '10 at 9:07a
activeJul 6, '10 at 5:15p
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase