471,072 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,072 software developers and data experts.

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

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
Jul 18 '05 #1
3 1868
Neil Zanella wrote:
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()


You need Metaclasses for that. Consider:
class PrintTest(object): .... class __metaclass__(type):
.... def __str__(self):
.... return "I'm a PrintTest"
.... print PrintTest I'm a PrintTest


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
Jul 18 '05 #2
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:
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.

....

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

Jul 18 '05 #3
Neil Zanella wrote:
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


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:
class FooType(type): .... def __str__(self):
.... return "custom text for class %s" % self.__name__
.... class Foo: .... __metaclass__ = FooType
.... def __str__(self):
.... return "custom text for %s instance" %
self.__class__.__name__
.... print Foo() custom text for Foo instance print Foo custom text for class Foo


Peter

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jay Moore | last post: by
1 post views Thread by Jordan | last post: by
3 posts views Thread by Tambi | last post: by
2 posts views Thread by wrytat | last post: by
reply views Thread by shapper | last post: by
7 posts views Thread by bukzor | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.