Printing data members of a class

John Fisher jfisher at are.berkeley.edu
Fri Sep 3 15:51:01 EDT 1999


In article <m3iu5ucidh.fsf at atrus.jesus.cam.ac.uk>,
  Michael Hudson <mwh21 at cam.ac.uk> wrote:
> John Fisher <jfisher at are.berkeley.edu> writes:

> >
> > >>> class x:
> > ... 	data = array([1, 2])
>
> Honk! Honk! You don't actually write it like that do you? You're
> setting yourself up for the famous mutable class attribute problem.

Oh, no!  The utility of that class would be pretty limited anyway.  I
was just trying to fabricate the simplest example that illustrated my
problem.

> A point to bear in mind: __repr__ must return a string. Don't do this:
>
> class x:
>     ...
>     def __repr__(self):
>         print self.data
>
> do this:
>
> class x:
>     ...
>     def __repr__(self):
>         return `self.data`
>
> This trips me up every now and again.

Heh heh.  How about:
class x:
    ...
    def __repr__(self):
        print self.data,
        return ''

But seriously, thanks for the replies, guys.

> HTH,
> Michael
>

John


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list