[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

constructor question

azrael

1/21/2008 10:52:00 PM

lets supose i have a object

>>>class a:
>>> __init__(self,b):
>>> self.b=b

>>>object=a(2)


how can I bind the object with "print". I supose that this should be
possible with a constructor. but I don't know how.

>>>print a
2
>>>

Something like this


Thnx
2 Answers

TeroV

1/21/2008 11:10:00 PM

0

azrael wrote:
> lets supose i have a object
>
>>>> class a:
>>>> __init__(self,b):
>>>> self.b=b
>
>>>> object=a(2)
>
>
> how can I bind the object with "print". I supose that this should be
> possible with a constructor. but I don't know how.
>
>>>> print a
> 2
>
> Something like this
>
>
> Thnx

Is it that you want to print an object, and customize what is printed?
__str__ method is what you need, see
http://docs.python.org/ref/customiz...

--
Tero

azrael

1/21/2008 11:35:00 PM

0

5 days ago I looked at it and didn't take a notice. thnx, this helped



On Jan 22, 12:10 am, TeroV <te...@nowhere.invalid> wrote:
> azrael wrote:
> > lets supose i have a object
>
> >>>> class a:
> >>>>   __init__(self,b):
> >>>>        self.b=b
>
> >>>> object=a(2)
>
> > how can I bind the object with "print". I supose that this should be
> > possible with a constructor. but I don't know how.
>
> >>>> print a
> > 2
>
> > Something like this
>
> > Thnx
>
> Is it that you want to print an object, and customize what is printed?
> __str__ method is what you need, seehttp://docs.python.org/ref/customiz...
>
> --
> Tero