Good or bad use of __repr__?

Terry Reedy tjreedy at udel.edu
Wed Feb 4 00:54:19 EST 2009


Alaric Haag wrote:
> Hello,
> 
> Is the use of __repr__ below a "really bad idea"? 
> 
> class Dimension():
>     def __init__(self, setp, name):
>         ptr = setp.contents.dim
>         while ptr.contents.name != name:
>             ptr = ptr.contents.next
>         self.name = ptr.contents.name
>         self.size = ptr.contents.size
>         self.unlimited = bool(ptr.contents.unlimited)
>         self.coord = ptr.contents.coord
>     def __repr__(self):
>         return '%g' % (self.size)
> 
> As written, if a program references a Dimension instance without an 
> attribute, it gets the size attrbute "by default".

No it does not.  It gets a string representation of the size.  Not the 
same thing.




More information about the Python-list mailing list