Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > using metaclass __call__ to replace class instance

Reply
Thread Tools

using metaclass __call__ to replace class instance

 
 
Ksenia Marasanova
Guest
Posts: n/a
 
      09-09-2005
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-09-2005
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


 
Reply With Quote
 
 
 
 
Ksenia Marasanova
Guest
Posts: n/a
 
      09-09-2005
2005/9/9, Peter Otten <__peter__@web.de>:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Metaclass of a metaclass Steven D'Aprano Python 1 06-05-2012 03:30 PM
Add methods to a predefined class: on the class instance or metaclass? Eustaquio Rangel de Oliveira Jr. Ruby 4 07-05-2005 04:14 PM
metaclass that inherits a class of that metaclass? ironfroggy Python 16 06-03-2005 10:00 AM
Redefining __call__ in an instance Robert Ferrell Python 5 01-21-2004 09:34 PM
RE: Redefining __call__ in an instance Robert Brewer Python 1 01-16-2004 02:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57