using % operator to print possibly unitialized data attributes

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?

Thank you,
-Adam
 
L

Leif K-Brooks

Adam said:
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:
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)
 
A

Adam Monsen

Leif said:
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
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top