Pro Python remarks to math ed folks (pointers)

Kirby Urner urner at alumni.princeton.edu
Tue Aug 24 23:28:57 EDT 1999


Jean-Claude Rimbault <rimbault at cybercable.fr> wrote:

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

OK, why not?  I've added:

   def __repr__(self):
       return self.__class__.__name__ + str(self.data)

And to my Poly class:

   def __repr__(self):
       return self.__class__.__name__

>>> v  = vector.Vector(quad=(1,0,0,0))
>>> v
Vector(1, 0, 0, 0)
>>> v=v*2
>>> v=v+vector.Vector(quad=(0,1,0,2))
>>> v
Vector(2, 1, 0, 2)
>>> oTet = polys.Tetra()
>>> oTet
Tetra

Without a separate __str__ method, print has the same 
behavior as just entering the object name.  OK for my
purposes, but anyone can tweak my code.

Polyhedra have a lot of info associated with them.  My
__repr__ method might return a lot more data.  Pondering
this.

Kirby

PS:  also fixed a bug in Poly() class, which is an 
abstract class -- designed only to be subclassed and 
not used directly.

Before fix:

>>> oPoly = polys.Poly()
Traceback (innermost last):
  File "<pyshell#72>", line 1, in ?
    oPoly = polys.Poly()
  File "G:\python\polys.py", line 70, in __init__
    self.distill()  # distill edges and points from faces
  File "G:\python\polys.py", line 98, in distill
    for i in self.edges:
AttributeError: edges

After fix:

>>> oPoly = polys.Poly()
Abstract class






More information about the Python-list mailing list