Connecting Tech Pros Worldwide Forums | Help | Site Map

classes are objects... so how can we custom print them: we need a classmethod syntax

Neil Zanella
Guest
 
Posts: n/a
#1: Jul 18 '05
Hello,

In Python, classes are objects. But there is no way to custom print a class
object. This would require some syntax such as the one commented out below:
With the current "foo = classmethod(foo)" mechanism custom printing for
class objects is not possible.

#!/usr/bin/python

class Foo:
def __str__(self):
return "foo"
#def classmethod __str__(cls):
# return "pythons bite"

foo = Foo()
s = "hello %s!" % foo # custom text here
print s

print Foo # no custom text here possible it seems, unless we call
# a staticmethod such as Foo.printMe()

Regards,

Neil

Reinhold Birkenfeld
Guest
 
Posts: n/a
#2: Jul 18 '05

re: classes are objects... so how can we custom print them: we need a classmethod syntax


Neil Zanella wrote:[color=blue]
> Hello,
>
> In Python, classes are objects. But there is no way to custom print a class
> object. This would require some syntax such as the one commented out below:
> With the current "foo = classmethod(foo)" mechanism custom printing for
> class objects is not possible.
>
> #!/usr/bin/python
>
> class Foo:
> def __str__(self):
> return "foo"
> #def classmethod __str__(cls):
> # return "pythons bite"
>
> foo = Foo()
> s = "hello %s!" % foo # custom text here
> print s
>
> print Foo # no custom text here possible it seems, unless we call
> # a staticmethod such as Foo.printMe()[/color]

You need Metaclasses for that. Consider:
[color=blue][color=green][color=darkred]
>>> class PrintTest(object):[/color][/color][/color]
.... class __metaclass__(type):
.... def __str__(self):
.... return "I'm a PrintTest"
....[color=blue][color=green][color=darkred]
>>> print PrintTest[/color][/color][/color]
I'm a PrintTest[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Mike C. Fletcher
Guest
 
Posts: n/a
#3: Jul 18 '05

re: classes are objects... so how can we custom print them: we need a classmethod syntax


From my metaclasses presentation:

"""Simple example of changing class repr"""
class Meta( type ):
def __repr__( cls ):
return '<OhLookAMetaClass>'
class X:
__metaclass__ = Meta

# this uses the meta-property for lookup
assert repr(X) == '<OhLookAMetaClass>'

Code and presentation available at:
http://www.vrplumber.com/programming/

HTH,
Mike

Neil Zanella wrote:
[color=blue]
>Hello,
>
>In Python, classes are objects. But there is no way to custom print a class
>object. This would require some syntax such as the one commented out below:
>With the current "foo = classmethod(foo)" mechanism custom printing for
>class objects is not possible.
>
>[/color]
....

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Peter Otten
Guest
 
Posts: n/a
#4: Jul 18 '05

re: classes are objects... so how can we custom print them: we need a classmethod syntax


Neil Zanella wrote:
[color=blue]
> Hello,
>
> In Python, classes are objects. But there is no way to custom print a
> class object. This would require some syntax such as the one commented out
> below: With the current "foo = classmethod(foo)" mechanism custom printing
> for class objects is not possible.
>
> #!/usr/bin/python
>
> class Foo:
> def __str__(self):
> return "foo"
> #def classmethod __str__(cls):
> # return "pythons bite"
>
> foo = Foo()
> s = "hello %s!" % foo # custom text here
> print s
>
> print Foo # no custom text here possible it seems, unless we call
> # a staticmethod such as Foo.printMe()
>
> Regards,
>
> Neil[/color]

Classes are objects. You have to define the __str__() method in the object's
class - for a class that would be the metaclass. Now here:
[color=blue][color=green][color=darkred]
>>> class FooType(type):[/color][/color][/color]
.... def __str__(self):
.... return "custom text for class %s" % self.__name__
....[color=blue][color=green][color=darkred]
>>> class Foo:[/color][/color][/color]
.... __metaclass__ = FooType
.... def __str__(self):
.... return "custom text for %s instance" %
self.__class__.__name__
....[color=blue][color=green][color=darkred]
>>> print Foo()[/color][/color][/color]
custom text for Foo instance[color=blue][color=green][color=darkred]
>>> print Foo[/color][/color][/color]
custom text for class Foo[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

Peter

Closed Thread