简体   繁体   中英

Python - passing a function (from scratch!) to another function?

Is it possible to pass a function written from scratch to another function or class instantiation?

This is what I have right now:

class Foo(Object):
    def __init__(self, name, somefunction):
        self.name = name
        self.somefunction = somefunction

def afunction(param1, param2):
    return param1 + param2

Bar = Foo("Something", afunction)

Bar.somefunction(2, 3)
#returns 5

I'm wondering whether it's possible to not have to declare afunction beforehand, but just write the code directly as a parameter to somefunction during instantiation. Something like this?

class Foo(Object):
    def __init__(self, name, somefunction):
        self.name = name
        self.somefunction = somefunction

Bar = Foo("Something", def afunction(param1, param2): return param1 + param2)

#OR

Bar = Foo("Something", Anonymousfunctiontechnique: return param1 + param2)

Bar.somefunction(2, 3)
#returns 5

And also, this is not a job for lambda! The functions may be pretty complex, which function calls inside of them and with multiple parameters. Also, I shouldn't be able to access it anywhere else (so a declaration outside won't work it seems).

Is something like this possible? If so, how? And if not, what would be the best way to do what I'm trying to do?

Thanks!!!

You cannot do this in Python beyond the limitations of lambda.

But you can define the function right above the call and pass it in, even if you are in another scope, making its name a temporary variable that does not pollute your address space.

The logical difference between

Bar = Foo("Something", def afunction(param1, param2): return param1 + param2)

and

def _foo(param1, param2): return param1 + param2
Bar = Foo("Something", _foo)

Is the same difference as between

Bar = foo(param1 + param2)

and

_good_name = param1 + param2
Bar = foo(_good_name)

Having names for any compound computation is a good idea for documentation purposes. It also helps people use any good debugger. And it keeps formatting considerations to a minimum. All told -- just name things .

The reason this is "left out" of Python comes down to:

  1. How many people would not assign an inline computation over three lines long to a local variable?
  2. How many of these functions are less than three lines long?
  3. So why bother?

(I always use an underscore doing this. The underscores mark these 'private', but they are not members, generally, so it just emphasizes that they are ephemeral. If the scoping forces them to be members, it clues people that they are internal and not for wider consumption. If you are totally paranoid about class clutter, use the same ones over and over and del them at the end of the class definition.).

What you're asking is really about a way to define functions inline.

Unfortunately, python does not support this. People have asked about this before.

In this PEP :

lambda will not be renamed.

At one point lambda was slated for removal in Python 3000. Unfortunately no one was able to come up with a better way of providing anonymous functions. And so lambda is here to stay.

But it is here to stay as-is. Adding support for statements is a non-starter. It would require allowing multi-line lambda expressions which would mean a multi-line expression could suddenly exist. That would allow for multi-line arguments to function calls, for instance. That is just plain ugly.

Thread: "genexp syntax / lambda", http://mail.python.org/pipermail/python-3000/2006-April/001042.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM