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

N

Neil Zanella

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
 
R

Reinhold Birkenfeld

Neil said:
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 __metaclass__(type):
.... def __str__(self):
.... return "I'm a PrintTest"
....
Reinhold
 
M

Mike C. Fletcher

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 said:
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
 
P

Peter Otten

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

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top