Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:45:47+00:00 2026-06-13T16:45:47+00:00

In a python discusion, I saw a function to convert IP string into an

  • 0

In a python discusion, I saw a function to convert IP string into an integer in functional progamming way. Here is the Link .

The function is implemented in a single line.

def ipnumber(ip):
    return reduce(lambda sum, chunk: sum <<8 | chunk, map(int, ip.split(".")))

However, I have few ideas of funcional programming. Could anybody explain the function in detail? I’ve some knowledge of “map” and “reduce”. But I don’t konw what “|” and “chunk” mean here.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:45:48+00:00Added an answer on June 13, 2026 at 4:45 pm

    sum and chunk are arguments to the lambda function passed to reduce. | is the binary or operator.

    The thing works like this:

    • ip.split(".") returns a list of strings, each corresponding to a piece of the dotted string ("192.168.0.1" => ["192", "168", "0", "1"];

    • map applies its first operand to each element of its second operand (["192", "168", "0", "1"] => [192, 168, 0, 1]);

    • reduce takes the first two arguments from the list and applies the lambda to them; then it does this again with the result of the lambda and the next element of the list; and so on.

    • the labmda function (an anonymous function defined on the spot) does this: takes the first argument, shifts it by eight bits and ORs to it the new chunk; thus, what happens is that the result is computed like:

      (((192<<8 | 168) << 8 | 0)<<8 | 1) = 192<<24 | 168<<16 | 0<<8 | 1
      

      which is exactly what the “dotted form” represents (it’s just a shorthand to indicate a 32 bit unsigned integer, which is what an IP is in IPv4 – you could say it’s a bit like expressing it in base 256)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here. I have a text file that is sorted into columns: fields
Python beginner here, I have a list of lists and want to refer to
Python neophyte here. I was wondering if someone could help with the KeyError I
As of 2.4 (2.6 for classes), python allows you to decorate a function with
I saw some other similar questions on this topic here but they were not
I am looking for any way to have Emacs format a Python buffer by
After seeing the discussion here: Python - generate the time difference I got curious.
One of the practices I have gotten into in Python from the beginning is
After reading: Dive into Python: Unicode Discussion I got curious to try printing my
I'm looking for a way to output a VERY simple pdf file from Python.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.