FAQ
I've wondering if there is a solution in 2.2 for this question... (or in 2.1
;)

I want a custom str rapresentation for a class object... something like
class A(object): pass
str(A)
<MyStrRapresentation of class A>

I think the best way to do this is with a class method, something like
class A(object):
... def __str__(klass):
... return "MyStrRapresentation of class %s" % klass.__name__
... __str__ = classmethod(__str__)
A.__str__()
"MyStrRapresentation of class A"
str(A)
'__main__.A'

I think it must pass for a metaclass, something like this
MyMeta(object):
... def __str__(self):
... return "MyStrRapresentation of ??"
class A(object):
... __metaclass__=MyMeta
str(A)
<MyStrRapresentation of ??>

that is quite fine, but cannot "fill it" with something usefull like ids or
so...

Any idea? Or better, someone can point me where I'm doing wrong?
Thanks in advance!

Paolo Invernizzi

Search Discussions

  • Markus Schaber at Sep 24, 2001 at 5:16 pm

    Paolo Invernizzi <paoloinvernizzi at dmsware.com> schrub:

    I've wondering if there is a solution in 2.2 for this question... (or
    in 2.1 ;)

    I want a custom str rapresentation for a class object... something
    like
    class A(object): pass
    str(A)
    <MyStrRapresentation of class A>
    try setting A.__repr__ to a method or function doing whatever you want.
    A.__repr__ = lambda: "hallo"
    print A
    hallo
    str(A)
    'hallo'

    Works with python 2.0.1 at my machine.

    markus
    --
    "The strength of the Constitution lies entirely in the determination of
    each citizen to defend it. Only if every single citizen feels duty
    bound to do his share in this defense are the constitutional rights
    secure." -- Albert Einstein
  • Paolo Invernizzi at Sep 25, 2001 at 7:54 am
    Markus Wrote...
    -------------
    try setting A.__repr__ to a method or function doing whatever you want.
    A.__repr__ = lambda: "hallo"
    print A
    hallo
    str(A)
    'hallo'
    -------------

    Things work differently in 2.2, and not only for "object" derived classes...
    (for now ;)

    Python 2.2a3 (#23, Sep 7 2001, 01:43:22) [MSC 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license" for more information.
    IDLE 0.8 -- press F1 for help
    class A: pass
    A.__repr__= lambda: "Repr of A"
    print A
    __main__.A
    str(A)
    '__main__.A'
    repr(A)
    '<class __main__.A at 0x009F8D2C>'
    class A(object): pass
    A.__repr__= lambda: "Repr of A"
    Traceback (most recent call last):
    File "<pyshell#10>", line 1, in ?
    A.__repr__= lambda: "Repr of A"
    TypeError: can't set type attributes

    Paolo Invernizzi
  • Martin von Loewis at Sep 25, 2001 at 1:22 pm

    "Paolo Invernizzi" <paoloinvernizzi at dmsware.com> writes:

    I've wondering if there is a solution in 2.2 for this question... (or in 2.1
    ;)

    I want a custom str rapresentation for a class object...
    Perhaps you could elaborate why you want this? Would it be sufficient
    if the object isn't a class object at all, but merely callable? Then,
    in 2.1, you can do

    class Meta:
    def __init__(self,klass):
    self.klass=klass
    def __repr__(self):
    return "special"
    def __call__(self,*args):
    return self.klass(*args)

    class _Klasse:
    pass

    Klasse = Meta(_Klasse)

    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>

    So a metaclass ought to be similar to the type type (which is bound to
    the type builtin); thus t inherits from type. Furthermore, the proper
    class c has object as a base class, but t as the metaclass.

    Regards,
    Martin
  • Paolo Invernizzi at Sep 25, 2001 at 3:08 pm
    Thanks for the advices!
    But...
    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)
    A.myStr()
    Special class A
    A().myStr()
    Instance of special class A

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

    Paolo Invernizzi
  • Martin von Loewis at Sep 25, 2001 at 3:50 pm

    "Paolo Invernizzi" <paoloinvernizzi at dmsware.com> writes:

    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>>
    But it's easy:

    class t(type):
    def __repr__(self):
    return '<special class %s at %x>' % (self.__name__,id(self))

    class c(object):
    __metaclass__=t

    print `c`

    gives

    <special class c at f7e88>

    Notice how the self argument of the metatype's method is the class
    object.

    Regards,
    Martin
  • Paolo Invernizzi at Sep 25, 2001 at 4:17 pm

    But it's easy:
    Notice how the self argument of the metatype's method is the class
    object.

    Regards,
    Martin
    Bingo!

    ;-((((((

    I was thinking the self argument of the metatype was the metatype instance!

    Thanks for the clarification!

    Paolo Invernizzi
  • Martin von Loewis at Sep 26, 2001 at 3:29 pm

    "Paolo Invernizzi" <paoloinvernizzi at dmsware.com> writes:

    Notice how the self argument of the metatype's method is the class
    object.

    Regards,
    Martin
    Bingo!

    ;-((((((

    I was thinking the self argument of the metatype was the metatype instance!
    As a matter of fact, it is: a metaclass instance is a class (just as a
    metametaclass instance would be a metaclass :-)

    Regards,
    Martin

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 24, '01 at 4:34p
activeSep 26, '01 at 3:29p
posts8
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase