#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13

    Inside the memory


    Hi,

    Many a times when we want to view what exactly is present in an object we get................

    Code:
    <xml.dom.minidom.Document instance at 0x011EDD78>
    It does differ for other objects....I wanted to know if there is a way out by which I cld view the info in that object....or maybe at that particular memory location..... is that possible?????

    Rgds,
    Subha
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What we need here is to overload the __repr__ method, or if we want to print the value the __str__ method. Like you mentioned the default representation (inherited from object) returns the objects name and memory address, which isn't exactly useful .

    Anyway, this should help you understand these methods and what they're used for:

    Code:
    >>> class Bar:
    ...     def __init__(self, foo):
    ...         self.foo = foo
    ... 
    >>> b = Bar('String Value')
    >>> b
    <__main__.Bar instance at 0x62210>
    >>> 
    >>> class Bar:
    ...     def __init__(self, foo):    
    ...         self.foo = foo
    ...     def __str__(self):
    ...         return str(self.foo)
    ... 
    >>> b = Bar('String Value')
    >>> b
    <__main__.Bar instance at 0x62440>
    >>> print b
    String Value
    >>> 
    >>> class Bar:
    ...     def __init__(self, foo):
    ...         self.foo = foo
    ...     def __str__(self):
    ...         return str(self.foo) 
    ...     def __repr__(self):
    ...         return str(self.foo)
    ... 
    >>> b = Bar('String Value')
    >>> b
    String Value
    >>> print b
    String Value
    >>>
    If you wanted to replace a method in a target class then the easiest way to do this would be to inherit from the target class and overload the methods your interested in [__repr__ and or __str__].

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    If you want a list of the attributes on an object you can use dir().

    If you want more detailed information you can use the inspect module.

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    I guess I've to be more clear with my question here!

    Code:
    >>> doc="""<?xml version="1.0" encoding="UTF-8"?>
    ... <verse>
    ... 	<attribution>Subha</attribution>
    ... 	<line>I believe in angels something good in evrything I see</line>
    ... </verse>
    ... """
    >>> from xml.dom import minidom
    >>> doc_node=minidom.parseString(doc)
    >>> doc_node
    <xml.dom.minidom.Document instance at 0x011BF710>
    So this is the problem. Not a big one though, but wld like to know the content of doc_node. How do I do that???

    Rgds,
    Subha
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    A quick search of the Python website would have revealed to you the XML sig and it's documentation:
    http://pyxml.sourceforge.net/topics/howto/xml-howto.html

    grim
    Last edited by Grim Archon; November 10th, 2004 at 05:35 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    What shd I say...unfortunately I'm a member of the XML SIG but then I still have not got my first query answered!

    And anyway, I guess my question remains same for other objects which produce the same kind of output on screen...not just the XML objects....let it pass!!

    Thanks,
    Subha
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    http://diveintopython.org/xml_processing/parsing_xml.html
    has a walk through.

    If you still have problems get back to us
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Thanks Grim! I'm going thru' that too....will get back if my doubts still exist!

    Thanks,
    Subha
  16. #9
  17. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    When you get something like:
    <xml.dom.minidom.Document instance at 0x011EDD78>
    or a some other such statement all you can say is that it is a reference to an object - unless you recognise the object you cannot possibly know how that object is structured internally.

    The object might be an xml thingy, equally it could be a reference to a regular expression match or an instance of a C extension module. These objects are not related in any way and their internal structure is totally different. So dissecting these objects in a generic way is unlikely to be useful.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Thanks Grim Its clear!

    Subha
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    You can view the methods/attributes of any object using the dir() command.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.

IMN logo majestic logo threadwatch logo seochat tools logo