473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1952
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay Moore | last post by:
I hope this isn't asking too much but here goes: Can someone explain to me in a simple way on how to use classes, and -- more importantly -- WHY to use them? I'm not a complete rookie to PHP,...
7
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on...
1
by: Jordan | last post by:
I have written a small C# application that will print out labels from a little label printer we have. However the size (how long the label is) is determined by a database call. I need to...
3
by: Tambi | last post by:
How can I get an object address as IntPtr ? Somehow even in unsafe it's not working with objetcs... I'am trying to compare two objects that are not the same but could be from some point. ...
2
by: wrytat | last post by:
I have a web application. There's 1 part whereby I'll show a list of items (order). I did this by using classes and objects. I defined my own classes (called orderitem and ordermanager), and my...
0
by: shapper | last post by:
Hello, I need to create a custom SiteMapProvider. I searched in Google for examples but I am still completly lost. The custom SiteMapProvider I need to create is exactly the same as the...
2
by: djsdaddy | last post by:
Good Day to All, I am organizing some Affirmative Action data and I need to be able to sum a number of field totals and then print them in a report. I have 5 tables that store the data that I need...
9
by: rushdi | last post by:
hi, i am trying to convert nos to a special format then print them into a text file. is it possible ? can anyone help me please ?
7
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.