467,166 Members | 1,082 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

using % operator to print possibly unitialized data attributes

The following code uses the % operator to print possibly unitialized
data attributes:
------------------------8<------------------------
class J:
name = ''
value = ''
def __str__(self):
vals = self.__class__.__dict__
vals.update(self.__dict__)
return 'name="%(name)s" value="%(value)s' % vals

j = J()
j.name = "j object"
print j
------------------------>8------------------------

A couple of questions:
* is there a simpler or more elegant way to do this?
* how can I get this to work for new-style classes?

Thank you,
-Adam

--
Adam Monsen <ha*****@gmail.com>
http://adammonsen.com/

Sep 9 '05 #1
  • viewed: 1031
Share:
3 Replies
Adam Monsen wrote:
class J:
name = ''
value = ''
def __str__(self):
vals = self.__class__.__dict__
vals.update(self.__dict__)
return 'name="%(name)s" value="%(value)s' % vals


This will update the class's attributes with instance attributes when
str() is called, which probably isn't what you want. For instance:
foo = J()
foo.name = "Joe Bloggs"
print foo name="Joe Bloggs" value=" bar = J()
print bar

name="Joe Bloggs" value="

What's wrong with the obvious version:

class J(object):
name = ''
value = ''
def __str__(self):
return 'name=%r value=%r' % (self.name, self.value)
Sep 9 '05 #2
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/Coo.../Recipe/137951
(shortened: http://snipurl.com/hka7 )

Thanks!
-Adam

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

Sep 9 '05 #3
[Adam Monsen]
The following code uses the % operator to print possibly unitialized
data attributes:
------------------------8<------------------------
class J:
name = ''
value = ''
def __str__(self):
vals = self.__class__.__dict__
vals.update(self.__dict__)
return 'name="%(name)s" value="%(value)s' % vals j = J()
j.name = "j object"
print j
------------------------>8------------------------ A couple of questions:
* is there a simpler or more elegant way to do this?
* how can I get this to work for new-style classes?


One solution which I used a few times, and which also opens the way to
many other niceties, is to manage so `vals' is a `dict'-like type of
your own. Then, you write its `__getitem__' method the way you want.

If I remember well, one of the niceties is that whenever `%(EXPR)s'
is used in a format string, EXPR may be a string (well balanced with
regard to parentheses) which you may then choose to "evaluate", for any
definition of "evaluate" which is fruitful for your application. :-)

--
François Pinard http://pinard.progiciels-bpi.ca
Sep 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michael Sparks | last post: by
6 posts views Thread by Ivan A. Kosarev | last post: by
reply views Thread by Kamilche | last post: by
7 posts views Thread by fakeprogress@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.