472,784 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 1922
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.