Special Method and Class

Y

Yermat

Hi all,

Why does special methods not work on class ? Or how to make them work ?
Here a simple example :
.... def __repr__(cls):
.... return "<special %s>" % cls.__name__
.... __repr__ = classmethod(__repr__)
.... def __str__(cls):
.... return "<test %s>" % cls.__name__
.... __str__ = classmethod(__str__)
....<special Test>

In fact, what I want to do is with __iter__ :

import weakref
class RememberInstance(object):
instances = []

def __init__(self,name):
self.name = name
self.__class__.instances.append(weakref.ref(self))

def __iter__(cls):
for wref in cls.instances:
inst = wref()
if inst:
yield inst
__iter__ = classmethod(__iter__)


t1 = RememberInstance('t1')
t2 = RememberInstance('t2')

for inst in RememberInstance:
print inst.name

Yermat
 
J

Jack Diederich

This is your clue, this didn't print <special Test>
for the same reason the __iter__ does work below.
The special __method__'s only work for instances of
the class and not for the class itself.

<special Test>

This deserves a better explanation,
repr(some_instance) will call the __repr__ method of the thing that foo
is an instance of.

You normally think of objects as instances of a class, but classes are
also an instances of a type. Now you're in metaclass territory which
is overkill for what you want to do.

illustration:

class MetaFoo(type):
"""define our own type"""
def __repr__(self):
return 'this is an instance of MetaFoo'

class Foo(object):
"""define a class which is an instance of our own type"""
__metaclass__ = MetaFoo
def __repr__(self):
return 'this is an instance of Foo'
'this is an instance of Foo'

I can't think of a reason that classmethod() would be useful or legal
when applied to magic methods like __repr__. Maybe it should throw
an exception instead. It is possible to do things with a classmethod
__repr__ but it is a very obscure way to get the end effect.

def Foo(object):
which = 'class'
def __init__(self):
self.which = 'instance'
def __repr__(cls):
return "Foo with which of " + cls.which
__repr__ = classmethod(__repr__)
'Foo with which of class'


-jackdied
 
Y

Yermat

Jack said:
This is your clue, this didn't print <special Test>
for the same reason the __iter__ does work below.
The special __method__'s only work for instances of
the class and not for the class itself.



This deserves a better explanation,
repr(some_instance) will call the __repr__ method of the thing that foo
is an instance of.

You normally think of objects as instances of a class, but classes are
also an instances of a type. Now you're in metaclass territory which
is overkill for what you want to do.

illustration:

class MetaFoo(type):
"""define our own type"""
def __repr__(self):
return 'this is an instance of MetaFoo'

class Foo(object):
"""define a class which is an instance of our own type"""
__metaclass__ = MetaFoo
def __repr__(self):
return 'this is an instance of Foo'



'this is an instance of MetaFoo'


'this is an instance of Foo'

I can't think of a reason that classmethod() would be useful or legal
when applied to magic methods like __repr__. Maybe it should throw
an exception instead. It is possible to do things with a classmethod
__repr__ but it is a very obscure way to get the end effect.

def Foo(object):
which = 'class'
def __init__(self):
self.which = 'instance'
def __repr__(cls):
return "Foo with which of " + cls.which
__repr__ = classmethod(__repr__)



'Foo with which of class'


-jackdied


Ok thanks, I see my mistake !
I thought that those special method were bounded methods but they are not...
.... def __init__(self, name):
.... self.name = name
.... def __repr__(self):
.... return "toto('%s')" % self.name
....<bound method toto.__init__ of toto('titi')>

That also mean that I can't change the __repr__ for only one instance :.... return "anotherRepr('%s')" % self.name
...."toto('titi')"


Is it really consistent ?
Should'nt it be also bounded methods even for clas instead of using
MetaClass ?

Yermat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top