Dealing with the __str__ method in classes with lots of attributes

Cameron Simpson cs at zip.com.au
Fri May 11 18:44:34 EDT 2012


On 11May2012 15:40, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
| On 11/05/2012 15:32, Andreas Tawn wrote:
| > It's also helpful to not have to display every attribute, of which there may be dozens.
| 
| Do I detect a code smell here?

Not necessarily. (Well, yeah, "dozens" may indicate time to partition
stuff.) My "O" class (back in the thread) deliberately shows only the
[a-z]* attributes so the user gets the public object state without the
noise of private attributes.

For __repr__ I might print everything. Haven't gone that far yet.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

That is 27 years ago, or about half an eternity in computer years.
        - Alan Tibbetts



More information about the Python-list mailing list