Question on metaclasses

Discussion in 'Python' started by =?iso-8859-1?q?Steffen_Gl=FCckselig?=, Apr 24, 2005.

  1. Hello,

    I've been experimenting with metaclasses a bit (even though I am quite
    a newbie to python) and stumpled over the following problem in my code:

    class Meta(type):
    def __init__(cls, name, bases, dct):
    for attr, value in dct.items():
    if callable(value):
    dct[attr] = wrapper(value)

    wrapper adds debugging-information to methods of the class (at least
    that is my plan).

    Using dct[attr] = wrapper(value) does not result in wrapped methods,
    though. Using setattr(cls, attr, wrapper(value)) creates the desired
    effect, though.

    Why are the changes to dct not visible in the instantiated class? Is
    dct not the namespace of the class currently instantiated?


    best regards
    Steffen
     
    =?iso-8859-1?q?Steffen_Gl=FCckselig?=, Apr 24, 2005
    #1
    1. Advertising

  2. Steffen Glückselig wrote:

    > Hello,
    >
    > I've been experimenting with metaclasses a bit (even though I am quite
    > a newbie to python) and stumpled over the following problem in my code:
    >
    > class Meta(type):
    > def __init__(cls, name, bases, dct):
    > for attr, value in dct.items():
    > if callable(value):
    > dct[attr] = wrapper(value)
    >
    > wrapper adds debugging-information to methods of the class (at least
    > that is my plan).
    >
    > Using dct[attr] = wrapper(value) does not result in wrapped methods,
    > though. Using setattr(cls, attr, wrapper(value)) creates the desired
    > effect, though.
    >
    > Why are the changes to dct not visible in the instantiated class? Is
    > dct not the namespace of the class currently instantiated?


    You don't use metaclasses correctly I believe. Usage should look like this:

    class Foo(type):
    def __new__(cls, name, bases, dict):

    for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    cls.wrap(k,v,cls.get_directives(v), dict)

    return super(Foo, self).__new__(self, name, bases, dict)

    Notice the __new__ instead of __init__, and the call to (actually, through
    super) type.__new__

    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Apr 24, 2005
    #2
    1. Advertising

  3. Diez B. Roggisch wrote:
    > Steffen Glückselig wrote:
    >
    >> Hello,
    >>
    >> I've been experimenting with metaclasses a bit (even though I am quite
    >> a newbie to python) and stumpled over the following problem in my code:
    >>
    >> class Meta(type):
    >> def __init__(cls, name, bases, dct):
    >> for attr, value in dct.items():
    >> if callable(value):
    >> dct[attr] = wrapper(value)
    >>
    >> wrapper adds debugging-information to methods of the class (at least
    >> that is my plan).
    >>
    >> Using dct[attr] = wrapper(value) does not result in wrapped methods,
    >> though. Using setattr(cls, attr, wrapper(value)) creates the desired
    >> effect, though.
    >>
    >> Why are the changes to dct not visible in the instantiated class? Is
    >> dct not the namespace of the class currently instantiated?

    >
    > You don't use metaclasses correctly I believe. Usage should look like this:
    >
    > class Foo(type):
    > def __new__(cls, name, bases, dict):
    >
    > for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    > cls.wrap(k,v,cls.get_directives(v), dict)
    >
    > return super(Foo, self).__new__(self, name, bases, dict)

    ^^^^ ^^^^
    self is not bound in your method; use

    return super(Foo, cls).__new__(name, bases, dict)

    Reinhold
     
    Reinhold Birkenfeld, Apr 24, 2005
    #3
  4. Diez B. Roggisch wrote:
    >> class Foo(type):
    >> def __new__(cls, name, bases, dict):
    >>
    >> for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    >> cls.wrap(k,v,cls.get_directives(v), dict)
    >>
    >> return super(Foo, self).__new__(self, name, bases, dict)

    >
    > There is a confusion of self and cls above - rename self with cls.


    And remove the first argument to __new__.

    Reinhold
     
    Reinhold Birkenfeld, Apr 24, 2005
    #4
  5. > class Foo(type):
    > def __new__(cls, name, bases, dict):
    >
    > for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    > cls.wrap(k,v,cls.get_directives(v), dict)
    >
    > return super(Foo, self).__new__(self, name, bases, dict)


    There is a confusion of self and cls above - rename self with cls.
    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Apr 24, 2005
    #5
  6. Are wrap and get_directives somehow built-in? I couldn't find
    references to them.

    I've noticed, too, that using __new__ I can manipulate the dictionary
    resulting in the behavior I intented.

    I'd rather like to know: Why does it work in __new__ but not in
    __init__?

    And, stimulated by your response: Why is using __new__ superior to
    __init__?


    best regards
    Steffen
     
    =?iso-8859-1?q?Steffen_Gl=FCckselig?=, Apr 24, 2005
    #6
  7. Steffen Glückselig wrote:
    > Are wrap and get_directives somehow built-in? I couldn't find
    > references to them.
    >
    > I've noticed, too, that using __new__ I can manipulate the dictionary
    > resulting in the behavior I intented.
    >
    > I'd rather like to know: Why does it work in __new__ but not in
    > __init__?
    >
    > And, stimulated by your response: Why is using __new__ superior to
    > __init__?


    At short, __new__ is called on the class and must return the instance; that
    means that the instance isn't created yet.

    __init__, on the other hand, is called on the instance which is already created
    at that point. So changing dct in __init__ is pointless, because the instance
    (which is a class actually) is already created.

    Reinhold
     
    Reinhold Birkenfeld, Apr 24, 2005
    #7
  8. Reinhold Birkenfeld wrote:

    > Diez B. Roggisch wrote:
    >>> class Foo(type):
    >>> def __new__(cls, name, bases, dict):
    >>>
    >>> for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    >>> cls.wrap(k,v,cls.get_directives(v), dict)
    >>>
    >>> return super(Foo, self).__new__(self, name, bases, dict)

    >>
    >> There is a confusion of self and cls above - rename self with cls.

    >
    > And remove the first argument to __new__.


    Ehm, no, I don't think so. The code was copied and pasted from a working
    metaclass (at least I hope so...), and in the original code a underscore
    was used for the first argument. I've been following that bad habit for a
    while but recently started to be more following to the established
    conventions. So I rewrote that code on the fly.

    This is the full metaclass:

    class TransactionAware(type):
    TAS_REX = re.compile("tas::([^, ]+(, *[^, ]+)*)")


    WRAPPERS = {
    "create" : w_create,
    "active" : w_active,
    "bind" : w_bind,
    "sync" : w_sync,
    "autocommit" : w_autocommit,
    }
    def __new__(_, name, bases, dict):

    for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    _.wrap(k,v,_.get_directives(v), dict)

    return super(TransactionAware, _).__new__(_, name, bases, dict)

    def wrap(_, name, fun, ds, dict, level=0):
    if ds:
    d, rest = ds[0], ds[1:]
    key = "_taw_%s_%i" % (name, level)
    try:
    w_fun = _.WRAPPERS[d](key)
    dict[key] = fun
    _.wrap(name, w_fun, rest, dict, level+1)
    except KeyError, e:
    print "No transaction aware property named %s" % d
    raise e
    else:

    dict[name] = fun

    wrap = classmethod(wrap)

    def get_directives(_, v):
    try:
    doc = v.__doc__
    res = []
    if doc:
    for l in doc.split("\n"):
    m = _.TAS_REX.match(l.strip())
    if m:
    res += [s.strip() for s in m.group(1 ).split(",")]
    res.reverse()
    return res
    except KeyError:
    return []
    get_directives = classmethod(get_directives)



    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Apr 24, 2005
    #8
  9. So dct is something like a template rather than the __dict__ of the
    actual class?

    I'd assume that changing the content of a dict would be possible even
    after it has been assigned to some object (here, a class).


    thanks and best regards
    Steffen
     
    =?iso-8859-1?q?Steffen_Gl=FCckselig?=, Apr 24, 2005
    #9
  10. Diez B. Roggisch wrote:
    > Reinhold Birkenfeld wrote:
    >
    >> Diez B. Roggisch wrote:
    >>>> class Foo(type):
    >>>> def __new__(cls, name, bases, dict):
    >>>>
    >>>> for k,v in [(k, v) for k,v in dict.items() if callable(v)]:
    >>>> cls.wrap(k,v,cls.get_directives(v), dict)
    >>>>
    >>>> return super(Foo, self).__new__(self, name, bases, dict)
    >>>
    >>> There is a confusion of self and cls above - rename self with cls.

    >>
    >> And remove the first argument to __new__.

    >
    > Ehm, no, I don't think so. The code was copied and pasted from a working
    > metaclass (at least I hope so...), and in the original code a underscore
    > was used for the first argument. I've been following that bad habit for a
    > while but recently started to be more following to the established
    > conventions. So I rewrote that code on the fly.


    Oh yes. I forgot that __new__ is a staticmethod anyhow.

    Reinhold
     
    Reinhold Birkenfeld, Apr 24, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Diehl

    question about metaclasses

    Stephan Diehl, Jun 30, 2003, in forum: Python
    Replies:
    2
    Views:
    338
    Stephan Diehl
    Jun 30, 2003
  2. Simon Burton

    metaclasses

    Simon Burton, Jul 28, 2003, in forum: Python
    Replies:
    5
    Views:
    396
    Simon Burton
    Jul 30, 2003
  3. Mike C. Fletcher

    Metaclasses presentation slides available...

    Mike C. Fletcher, Aug 28, 2003, in forum: Python
    Replies:
    3
    Views:
    338
    Alex Martelli
    Aug 29, 2003
  4. Laszlo Nagy

    metaclasses (beginner question)

    Laszlo Nagy, Feb 21, 2007, in forum: Python
    Replies:
    4
    Views:
    271
    James Stroud
    Feb 22, 2007
  5. Darren Dale

    design question, metaclasses?

    Darren Dale, Apr 11, 2009, in forum: Python
    Replies:
    7
    Views:
    305
    Piet van Oostrum
    Apr 14, 2009
Loading...

Share This Page