A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/331436.html below:

using % operator to print possibly unitialized data attributes

using % operator to print possibly unitialized data attributes using % operator to print possibly unitialized data attributesAdam Monsen haircut at gmail.com
Fri Sep 9 16:30:24 EDT 2005
Leif K-Brooks wrote:
> This will update the class's attributes with instance attributes
> when str() is called, which probably isn't what you want.
[...]

Yikes, you're right! Well, I figured out a modification to my original
__str__ code that works for old and new-style classes which doesn't
overwrite the __class__.__dict__:

class J(object):
    name = ''
    value = ''
    def __str__(self):
        vals = dict(self.__class__.__dict__)
        vals.update(self.__dict__)
        return 'name="%(name)s" value="%(value)s' % vals


> What's wrong with the obvious version:
[...]

Oh, that looks nice and clean. I like it.

I also found a recipe in the Python cookbook that works great for
"dumping" objects:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/137951
(shortened: http://snipurl.com/hka7 )

Thanks!
-Adam

-- 
Adam Monsen
http://adammonsen.com/


More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4