Search code examples
pythondictionarydnsdnspython

python output different when printed to std out than stored in dict


I'm trying to do a zonetransfer with dnspython, and then store the A record's into a dictionary that i can pop values from later. See below:

import dns.zone
import dns.query
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *

domain = "mydomain.com"
ns = '1.2.3.4'

try:
    zone = dns.zone.from_xfr(dns.query.xfr(ns, domain))
    print "Zone origin:", zone.origin
except DNSException, e:
    print e.__class__, e

for (name, ttl, rdata) in zone.iterate_rdatas('A'):
    record = { name : rdata }
    print name, rdata
    print record

why is the output when i use print is different than when i store the same variables name, rdata in a dictionary, and print that dict? See output below:

www 1.1.1.1
{<DNS name www>: <DNS IN A rdata: 1.1.1.1>}

I guess to clarify my question: How do I make the dict look like : { 'www' : '1.1.1.1' }


Solution

  • This is happening because the contents of a dict are represented by calling repr on each of its keys/values, whereas printing an object directly will call str.

    Example:

    >>> class Fred:
    ...     def __repr__(self):
    ...         return "value returned by repr"
    ...     def __str__(self):
    ...         return "value returned by str"
    ...
    >>> d = {1: Fred()}
    >>> print d
    {1: value returned by repr}
    >>> print d[1]
    value returned by str
    

    There's not a whole lot you can do about this... You could write your own version of the classes you're trying to print and define __repr__ so it behaves identically to __str__. Or you could write a custom dict-printing function that renders the result of __str__ instead of __repr__. But there's no built-in way to force print somedict to use __str__ instead of __repr__.

    >>> print "{" + ", ".join("{} : {}".format(repr(str(key)), repr(str(value))) for key, value in d.iteritems()) + "}"
    {'1' : 'value returned by str'}