What does the _ do in ipstring(_)?
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
In command line you get the last result from the last command from variable _, In my program I usually use _ to denote the return values, that I have no use for, for example when I do partition and are only interested the third part, which is after cutting:
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: 2+3
Out[1]: 5
In [2]: _
Out[2]: 5
In [3]: a="Heikki Kovalainen, Finnish Formula 1 driver"
In [4]: _,_,prof=a.partition(',')
In [5]: prof
Out[5]: ' Finnish Formula 1 driver'
In [6]:
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Doesn't look like Python at all!
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
That is IPython shell, which have much extra functionality, basically can give OS commands and Python commands both.
but same is applicable Idle, Python prompt etc.
And that double was same post again, but was no way to delete it even button EDIT/DELETE.
If you want to prove IPython, you can find it in http://ipython.scipy.org/moin/Download
Or you can do
python easy_install ipython
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
This one month old newbie member did clear code by his standards (as old Lisper I do get the meaning of the code you posted, but it is not idiomatic Python). This senior poster would say:
def ipnumber(ip):
return sum(256**power*int(value) for power, value in enumerate(reversed(ip.rstrip().split('.'))))
def ipstring(ip):
return '.'.join('%s' % (ip // 256**ind % 256) for ind in reversed(range(4)))
if __name__ == '__main__':
testcase = '211.234.32.123'
r = ipnumber(testcase)
print('%s = %s = %s' % (testcase, r, ipstring(r)))
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852