python 2.2 question...

Paolo Invernizzi paoloinvernizzi at
Tue Sep 25 17:08:17 CEST 2001

Thanks for the advices!

> Would it be sufficient if the object isn't a class object at all, but
merely callable?
>Then, in 2.1, you can do
Unfortunatly the object is a class....

> If you absolutely want to do this with 'true' meta classes, this is
> the 2.2 way of writing it:
> >>> class t(type):
> ...   def __repr__(self):return 'special'
> ...
> >>> class c(object):
> ...   __metaclass__=t
> ...
> >>> c
> special
> >>> o=c()
> >>> o
> <__main__.c object at 0x18da10>

And here we are again at the second part of my original post...
The problem with metaclasses is that I cannot specify in the "special"
string anything related to the original class object...
The return value is "always" <special>, and I see no way to turn it in
something like <special class c id <0xAddress>>
I think the best way to do it is with classmethod, but str() or repr() on a
class object doesn't look for class/static __str__ or __repr__
For now I resolved with this....
class A(object):
    def __init__(self, line):
        myStr = self.myStrInstance
    def myStrInstance(self):
        return "Instance of special class %s" % self.__class__.__name__
    def myStrClass(klass):
        return "Special class %s" % klass.__name__
    myStr = classmethod(myStrClass)

Special class A
Instance of special class A

I guess I had to revert to a more tradictional approch if I cannot solve
Special class A
Instance of special class A

Paolo Invernizzi

More information about the Python-list mailing list