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 6757943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:42:56+00:00 2026-05-26T13:42:56+00:00

I’m revisiting some scheme excercises in python (if that makes sense) to find out

  • 0

I’m revisiting some scheme excercises in python (if that makes sense) to find out what python can do in terms of FP. My problem concerns lambda in python :
Can i define a general function in python with an operator as one of the arguments?

Think this :

def f (op,x,y):
    #return some lambda function that combines x and y in the appropriate way
    #i.e if op is +, then return x+y, if op is -, then return x-y etc

#Edit : added usage
#this should be called like this:
f(+, 1,2) #should return 3

I know this is possible in scheme, but is there something equivalent in python? I’ve gotten the impression that lambda in python is just a shorter way of defining a method, and I’ve not found any way to define a general combiner function in python.

  • 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-05-26T13:42:56+00:00Added an answer on May 26, 2026 at 1:42 pm

    I can see some points in your question, lets go through them in order:

    1. Can I pass a function as a parameter to someone?

    Yes:

    def f(op, x, y):
        return op(x, y)
    
    def add(x, y):
        return x + y
    
    f(add, 10, 7) #gives 17
    

    2. What about operators then?

    Unlike scheme, Python operators are not functions so you can’t pass them directly as parameters. You can either create the wrapper functions yourself or you can import the operator module from the standard library.

    import operator
    
    operator.add(1, 2)
    (lambda x,y : x+y)(1, 2)
    

    Operators not being real functions is a little sad in most cases but at least Python gives us chained comparisons like 10 <= x < 100 in exchange…

    3. So what is the difference between Python and Scheme then?

    In the general sense, functions in Python are as powerful as functions in Scheme, however there are some things to note:

    The lambda keyword is limited

    You can only have a single expression as the function body

    f = lambda x, y: x + y
    

    Since there are a bunch of things in Python that are statements and not expressions (assignments, the 2.x print, …), you often need to fall back to named functions instead.

    There are closures

    def make_printer(msg):
        def printer():
            print msg
        return printer
    
    printer('a message')()
    

    But mutating variables in them is a pain

    This doesn’t work. It tries to bind a new n for the inner function instead of using the outer one

    def make_counter(n):
        def inc():
            n = n + 1
            return n
        return inc
    

    new 3.x nonlocal keyword

    def make_counter(n):
        def inc():
            nonlocal n
            n = n + 1
            return n
        return inc
    

    workaround w/ mutable objects

    def make_counter(n):
        nw = [n]
        def inc():
           nw[0] = nw[0] + 1
           return nw[0]
        return inc
    

    Objects instead of closures. Uses the magic __call__ method to pretend its a function

    class Counter:
        def __init__(self, n):
            self.n = n
        def __call__(self):
            self.n += 1
            return self.n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

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.