[Tutor] Re: How to find out DNS ?

Kalle Svensson kalle@lysator.liu.se
Thu, 27 Jun 2002 21:03:45 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Derrick 'dman' Hudson (dman@dman.ddts.net) wrote:
> 
> > f = open( "/etc/resolv.conf" , "r" )
> > nameservers = []
> > for line in f.xreadlines() :

[Marc]
> Nice code Dman. But there are some lines I didnt understand.
> Why r u using xreadlines? What's wrong with good, old readlines?

If the file is large, readlines will consume very much memory.
Admittedly, this is unlikely when the file in question is
/etc/resolv.conf, but it's a good habit to use xreadlines.

> >     line = line.strip()
> >     if line.startswith( "nameserver" ) :
> >         _ , ip = line.split()
> 
> I did not understand the thing with _ . Can u explain?
> Thanks in advance and sorry If my questions are too dumb.

line.split() returns a two-element tuple,

("nameserver", "10.0.0.1")

but we're only interested in the IP address.
Note that this is a valid form of assignment:

_, ip = ("nameserver", "10.0.0.1")

I would perhaps have written it as

if line.startswith("nameserver"):
    ip = line.split()[1]

instead.  Also, the use of _ as the "throw-away" variable name is
perhaps a bit unfortunate, considering that the interactive
interpreter uses _ to save the result of the last expression:

>>> 3 * 15
45
>>> _
45

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9G2GKdNeA1787sd0RAuWnAJwPquq8/L1foCQtIN0gu3UFtoDv0wCfSxob
0FyuwO0h+XjSJrHoNvMQpbo=
=ow3b
-----END PGP SIGNATURE-----