|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 ![]() |
|
#5
|
||||
|
||||
|
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 ![]()
__________________
*** Experimental Python Markup CGI V2 *** Last edited by Grim Archon : November 10th, 2004 at 05:35 AM. |
|
#6
|
|||
|
|||
|
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 ![]() |
|
#7
|
||||
|
||||
|
http://diveintopython.org/xml_processing/parsing_xml.html
has a walk through. If you still have problems get back to us ![]() |
|
#8
|
|||
|
|||
|
Thanks Grim!
I'm going thru' that too....will get back if my doubts still exist!Thanks, Subha ![]() |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
|||
|
|||
|
Thanks Grim
Its clear!Subha ![]() |
|
#11
|
|||
|
|||
|
You can view the methods/attributes of any object using the dir() command.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Inside the memory |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|