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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:23:46+00:00 2026-05-11T19:23:46+00:00

I’m writing a function that exponentiates an object, i.e. given a and n, returns

  • 0

I’m writing a function that exponentiates an object, i.e. given a and n, returns an. Since a needs not be a built-in type, the function accepts, as a keyword argument, a function to perform multiplications. If undefined, it defaults to the objects __mul__ method, i.e. the object itself is expected to have multiplication defined. That part is sort of easy:

def bin_pow(a, n, **kwargs) :

    mul = kwargs.pop('mul',None)
    if mul is None :
        mul = lambda x,y : x*y

The thing is that in the process of calculating an the are a lot of intermediate squarings, and there often are more efficient ways to compute them than simply multiplying the object by itself. It is easy to define another function that computes the square and pass it as another keyword argument, something like:

def bin_pow(a, n, **kwargs) :
    mul = kwargs.pop('mul',None)
    sqr = kwargs.pop('sqr',None)

    if mul is None :
        mul = lambda x,y : x*y
    if sqr is None :
        sqr = lambda x : mul(x,x)

The problem here comes if the function to square the object is not a standalone function, but is a method of the object being exponentiated, which would be a very reasonable thing to do. The only way of doing this I can think of is something like this:

import inspect

def bin_pow(a, n, **kwargs) :
    mul = kwargs.pop('mul',None)
    sqr = kwargs.pop('sqr',None)

    if mul is None :
        mul = lambda x,y : x*y
    if sqr is None :
        sqr = lambda x : mul(x,x)
    elif inspect.isfunction(sqr) == False : # if not a function, it is a string
        sqr = lambda x : eval('x.'+sqr+'()')

It does work, but I find it an extremely unelegant way of doing things… My mastery of OOP is limited, but if there was a way to have sqr point to the class’ function, not to an instance’s one, then I could get away with something like sqr = lambda x : sqr(x), or maybe sqr = lambda x: x.sqr(). Can this be done? Is there any other more pythonic way?

  • 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-11T19:23:46+00:00Added an answer on May 11, 2026 at 7:23 pm

    You can call unbound methods with the instance as the first parameter:

    class A(int):
        def sqr(self):
            return A(self*self)
    
    sqr = A.sqr
    a = A(5)
    print sqr(a) # Prints 25
    

    So in your case you don’t actually need to do anything specific, just the following:

    bin_pow(a, n, sqr=A.sqr)
    

    Be aware that this is early binding, so if you have a subclass B that overrides sqr then still A.sqr is called. For late binding you can use a lambda at the callsite:

    bin_pow(a, n, sqr=lambda x: x.sqr())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This might be related to the EOI marker at the… May 12, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer You do not state what you want to achieve by… May 12, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer There's no such thing a a void type in Delphi.… May 12, 2026 at 12:23 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.