using metaclass __call__ to replace class instance

Discussion in 'Python' started by Ksenia Marasanova, Sep 9, 2005.

  1. Hi all,

    I am creating some library, and want use "declarative" style in the
    subclasses as much as possible, while the actual use will be more
    method-like.

    Just to give an impression, the library would be something like this:

    class Baseclass(object):
    # lot's of code goes here...

    class Basemethod(object):
    foo = None
    def get_foo(cls):
    return cls.foo
    get_foo = classmethod(get_foo)

    The subclasses:

    class Class(Baseclass):

    class method1(Basemethod):
    foo = "foo1"

    class method2(Basemethod):
    foo = "foo2"


    And the actual use would be:

    print Class.method1()
    "foo1"

    print Class.method2()
    "foo2"

    So I thought that the way to accomplish it would be using metaclass
    __call__ method:

    class BasemethodMeta(type):
    def __new__(cls, class_name, bases, new_attrs):
    cls = type.__new__(cls, class_name, bases, new_attrs)
    new_attrs['__metaclass__'].cls = cls
    return cls

    def __call__(self):
    return self.cls.get_foo()

    class Basemethod(object):
    __metaclass__ = BasemethodMeta
    def get_foo(cls):
    return cls.foo
    get_foo = classmethod(get_foo)


    But it doesn't work as I expected:

    print Class.method1()
    "foo2"
    print Class.method2()
    "foo2"

    I understand now that because BasemethodMeta is *type* (not sure if
    this is the right word) for all Basemethod classes, it always
    returnes the latest declared class... Creating dictionary and putting
    all classes in it doesn't make much sense either, because
    BasemethodMeta still doesn't know what is the current class that is
    being called... (right?)
    Now I am stuck. Can anybody show me the light?

    Appreciate any help,
    --
    Ksenia
     
    Ksenia Marasanova, Sep 9, 2005
    #1
    1. Advertising

  2. Ksenia Marasanova

    Peter Otten Guest

    Ksenia Marasanova wrote:

    > class BasemethodMeta(type):
    > def __new__(cls, class_name, bases, new_attrs):
    > cls = type.__new__(cls, class_name, bases, new_attrs)
    > new_attrs['__metaclass__'].cls = cls
    > return cls
    >
    > def __call__(self):
    > return self.cls.get_foo()


    Though I'm not sure what you are trying to do, I fear it will get more
    complicated than necessary. But you do get the desired output if
    you change your metaclass to

    class BasemethodMeta(type):
    def __call__(cls):
    # the instance of the metaclass already is a class
    # so you can drop the double indirection
    return cls.get_foo()

    Peter
     
    Peter Otten, Sep 9, 2005
    #2
    1. Advertising

  3. 2005/9/9, Peter Otten <>:
    > Ksenia Marasanova wrote:
    >
    > > class BasemethodMeta(type):
    > > def__new__(cls,class_name,bases,new_attrs):
    > > cls=type.__new__(cls,class_name,bases,new_attrs)
    > > new_attrs['__metaclass__'].cls=cls
    > > returncls
    > >
    > > def__call__(self):
    > > returnself.cls.get_foo()

    >
    > Though I'm not sure what you are trying to do, I fear it will get more
    > complicated than necessary. But you do get the desired output if
    > you change your metaclass to
    >
    > class BasemethodMeta(type):
    > def __call__(cls):
    > # the instance of the metaclass already is a class
    > # so you can drop the double indirection
    > return cls.get_foo()
    >
    > Peter
    >



    Man.. that's easy. Thanks a lot, Peter.

    --
    Ksenia
     
    Ksenia Marasanova, Sep 9, 2005
    #3
    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. Robert Ferrell

    Redefining __call__ in an instance

    Robert Ferrell, Jan 15, 2004, in forum: Python
    Replies:
    5
    Views:
    371
    John Roth
    Jan 21, 2004
  2. ironfroggy
    Replies:
    16
    Views:
    442
    Michele Simionato
    Jun 3, 2005
  3. Eustaquio Rangel de Oliveira Jr.
    Replies:
    4
    Views:
    157
    Robert Klemme
    Jul 5, 2005
  4. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    310
  5. Marco Buttu
    Replies:
    3
    Views:
    149
    Marco Buttu
    Oct 7, 2013
Loading...

Share This Page