using % operator to print possibly unitialized data attributes

Discussion in 'Python' started by Adam Monsen, Sep 9, 2005.

  1. Adam Monsen

    Adam Monsen Guest

    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 <>
    http://adammonsen.com/
    Adam Monsen, Sep 9, 2005
    #1
    1. Advertising

  2. 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)
    Leif K-Brooks, Sep 9, 2005
    #2
    1. Advertising

  3. Adam Monsen

    Adam Monsen Guest

    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/
    Adam Monsen, Sep 9, 2005
    #3
  4. [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
    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Sep 9, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrell Grainger

    Re: Unitialized Memory Read In sscanf()

    Darrell Grainger, Jun 24, 2003, in forum: C Programming
    Replies:
    1
    Views:
    819
    Alan Balmer
    Jun 24, 2003
  2. Eric Sosman

    Re: Unitialized Memory Read In sscanf()

    Eric Sosman, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    845
    Eric Sosman
    Jun 24, 2003
  3. Greg

    Unitialized pointers?

    Greg, Oct 11, 2004, in forum: C Programming
    Replies:
    5
    Views:
    330
  4. Replies:
    2
    Views:
    299
  5. Michael Linfield

    Ruport::DataSet unitialized constant?

    Michael Linfield, Aug 15, 2007, in forum: Ruby
    Replies:
    1
    Views:
    113
    Stefan Mahlitz
    Aug 15, 2007
Loading...

Share This Page