By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

using % operator to print possibly unitialized data attributes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
[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.