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 2 2275
Ksenia Marasanova wrote: class BasemethodMeta(type): def*__new__(cls,*class_name,*bases,*new_attrs): cls*=*type.__new__(cls,*class_name,*bases,*new_att rs) 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
2005/9/9, Peter Otten <__*******@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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Patrick Lioi |
last post by:
def foo(): pass
foo is a function
foo is a callable object
foo has method __call__ defined
foo.__call__ is a function
foo.__call__ is a callable object
foo.__call__ has method __call__...
|
by: Robert Ferrell |
last post by:
I have a question about assigning __call__ to an instance to make that
instance callable. I know there has been quite a bit of discussion
about this, and I've read all I can find, but I'm still...
|
by: Lenard Lindstrom |
last post by:
I was wondering if anyone has suggested having Python determine
a method's kind from its first parameter. 'self' is a de facto
reserved word; 'cls' is a good indicator of a class method
( __new__...
|
by: Paul Morrow |
last post by:
One of the beautiful things about Python is its clear, minimal syntax.
So we must resist adding new syntax to the language, especially where
there is a reasonable alternative.
I believe that...
|
by: Stefan Behnel |
last post by:
Hi!
This somewhat puzzles me:
Python 2.4 (#1, Feb 3 2005, 16:47:05)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
..>>> class test(object):
.... def...
|
by: Anders Borum |
last post by:
Hello!
Whilst refactoring an application, I was looking at optimizing a
ModelFactory with generics. Unfortunately, the business objects created by
the ModelFactory doesn't provide public...
|
by: Christian Eder |
last post by:
Hi,
I think I have discovered a problem in context of
metaclasses and multiple inheritance in python 2.4,
which I could finally reduce to a simple example:
Look at following code:
class...
|
by: sashang |
last post by:
Hi
I'd like to use metaclasses to dynamically generate a class based on a
parameter to the objects init function.
For example:
class MetaThing(type):
def __init__(cls, name, bases, dict,...
|
by: Pedro Werneck |
last post by:
Hi all
I noticed something strange here while explaining decorators to someone.
Not any real use code, but I think it's worth mentioning.
When I access a class attribute, on a class with a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |