[ python-Bugs-884064 ] metaclasses and __unicode__

SourceForge.net noreply at sourceforge.net
Thu Feb 12 09:05:05 EST 2004


Bugs item #884064, was opened at 2004-01-25 10:24
Message generated for change (Comment added) made by mwh
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=884064&group_id=5470

Category: Type/class unification
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Christoph Simon (arundo)
>Assigned to: Michael Hudson (mwh)
Summary: metaclasses and __unicode__

Initial Comment:
class Metaklasse (type) :
    def __str__ (cls) :
        return 'string'
    def __unicode__ (cls) :
        return u'unicode'
    def __int__ (cls) :
        return 7

class Klasse :
    __metaclass__ = Metaklasse
    def __str__ (cls) :
        return 'text'
    def __unicode__ (cls) :
        return u'unicodetext'
    def __int__ (cls) :
        return 9

print str(Klasse)
#print unicode(Klasse)
print int(Klasse)

print str(Klasse())
print unicode(Klasse())
print int(Klasse())

print unicode(Klasse)

The last print statemant raises an error

Traceback (most recent call last):
  File "Metaklasse.py", line 26, in -toplevel-
    print unicode(Klasse)
TypeError: unbound method __unicode__() must be called 
with Klasse instance as first argument (got nothing 
instead)

unicode() does not use the __unicode__ function of the 
metaclass. But int() and str() do.

----------------------------------------------------------------------

>Comment By: Michael Hudson (mwh)
Date: 2004-02-12 14:05

Message:
Logged In: YES 
user_id=6656

Agree with etrepum.

----------------------------------------------------------------------

Comment By: Bob Ippolito (etrepum)
Date: 2004-01-26 08:00

Message:
Logged In: YES 
user_id=139309

This is a bug in your implementation, not in Python.  On your 
metaclass, __unicode__ must be a descriptor that looks 
something like this.

def metamethod(f, doc=None):
....def __get__(ob):
........return f.__get__(ob, ob.__class__)
....return property(__get__, doc=doc)

try it with the metaclass like this:

class Metaclass(type):
....def __unicode__(self):
........return u'metaclass'
....__unicode__ = metamethod(__unicode__)

(btw: to save you the trouble, it works)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=884064&group_id=5470



More information about the Python-bugs-list mailing list