473,405 Members | 2,349 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,405 software developers and data experts.

Is there a way to define a default print for a class?

4
I have a class and I am printing an instance of it:

Expand|Select|Wrap|Line Numbers
  1. class Complex:
  2.   def __init__( self, realpart, imagpart = 0 ):
  3.     self.r = realpart
  4.     self.i = imagpart
  5.  
  6.   def show( self ):
  7.     print "(" + str( self.r ) + ", " + str( self.i ) + ")"
  8.  
  9. x = Complex( 1, 2 )
  10. x.show()
  11. print x

This is what gets printed out:
Expand|Select|Wrap|Line Numbers
  1. (1, 2)
  2. <__main__.Complex instance at 0x0150C030>
  3.  
Is there a way that we can define a default way to print our class to make the "print x" line print the same way as the "x.show()"?
Aug 10 '07 #1
3 981
ilikepython
844 Expert 512MB
I have a class and I am printing an instance of it:

Expand|Select|Wrap|Line Numbers
  1. class Complex:
  2.   def __init__( self, realpart, imagpart = 0 ):
  3.     self.r = realpart
  4.     self.i = imagpart
  5.  
  6.   def show( self ):
  7.     print "(" + str( self.r ) + ", " + str( self.i ) + ")"
  8.  
  9. x = Complex( 1, 2 )
  10. x.show()
  11. print x

This is what gets printed out:
Expand|Select|Wrap|Line Numbers
  1. (1, 2)
  2. <__main__.Complex instance at 0x0150C030>
  3.  
Is there a way that we can define a default way to print our class to make the "print x" line print the same way as the "x.show()"?
Yea, you use the __str__ or __repr__:
Expand|Select|Wrap|Line Numbers
  1. class whatever:
  2.     def __str__(self):
  3.         return "Test"
  4.  
  5. x = whatever()
  6. print x
  7.  
Aug 11 '07 #2
POINTS
4
Wow thanks that works perfectly! I swear I checked google first...
Aug 11 '07 #3
bartonc
6,596 Expert 4TB
Wow thanks that works perfectly! I swear I checked google first...
That's probably how you ended up finding the best Python forum on the web...
Aug 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
2
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
14
by: ago | last post by:
Is it possible to have a default value associated python objects? I.e. to flag an attribute in such a way that the assignment operator for the object returns the default attribute instead of the...
18
by: **Developer** | last post by:
I always define events with the parameters ByVal sender As Object, ByVal e As EventArgs Even if they are not used. Seems I read someplace that's the thing to do. So I then do:
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
7
by: George Sakkis | last post by:
A situation that often comes up is having to initialize several instance attributes that accept a default value. For a single class, passing the default values in __init__ is fine: class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.