Pro Python remarks to math ed folks (pointers)

Jean-Claude Rimbault rimbault at cybercable.fr
Tue Aug 24 23:33:32 CEST 1999


Kirby Urner wrote:

> "Darrell" <news at dorb.com> wrote:
>
> >Installed your stuff and changed a few things to get it to work.
> >Had to add this method too Vector.
> >   def display(self):
> >       print self.data
>
> That's cool.
>
> I've just been going:
>
> from vector import Vector
> v = Vector(quad=(1,0,0,0))
> print v.data
> (1.0, 0.0, 0.0, 0.0)

You could also add a __repr__ method and __str__ method to Vector:

   def __str__(self):    # convert object to string (for printing)
      return `self.data`

   def __repr__(self):  # convert object to string
      return 'Vector'+`self.data`

Then you would be able to type:

   >>> v=Vector(quad=(1,0,0,0))
   >>> v
   Vector(1, 0, 0, 0)    # __repr__ method is called
  >>> print v
  (1, 0, 0, 0)                # __str__ method is called


Jean-Claude.





More information about the Python-list mailing list