-1

This may be a newbie question but I was wondering if it is possible to pass a method as a parameter to another method and get the result back?

Example

 def with_price(price: float, factor: float = 2.0):
        return (price * factor)
    
 def a_method(a: float, with_price):
        b = with_price(11)
        return a * b
 print(a_method)

Then I get this

<function main.<locals>.a_method at 0x0000023B24029EE0>

which is not what I expect.

Thanks

Mich Talebzadeh
  • 117
  • 2
  • 12
  • 2
    What do you expect? – JanLikar Dec 30 '21 at 23:18
  • 6
    You're not *calling* `a_method`. You need parentheses for that (and you also need to provide its parameters). Also you have an indentation error that must have happened in pasting here. – CrazyChucky Dec 30 '21 at 23:19
  • 1
    It looks like the indenting is wrong and `def main` is missing. Please [edit] to fix it. See [mre]. – wjandrea Dec 30 '21 at 23:20
  • 2
    Note, those arent methods – juanpa.arrivillaga Dec 30 '21 at 23:20
  • 2
    What are you actually trying to accomplish? Based on what you've written, it's not clear if you even need parameters, like maybe you just need to do `def a_method(...): b = with_price(11) ...`. Please [edit] to clarify. Be wary of the [XY problem](https://meta.stackexchange.com/q/66377/343832). For more tips, see [ask]. – wjandrea Dec 30 '21 at 23:42

3 Answers3

2

print(a_method) is telling Python to print what print_method is, which is a function. However, you want it to print what the function returns when called, so you should have used print(a_method()), since calling the function looks like a_method().

However, your code appears confused in a few more ways, so that's not even really the problem. Here's what I think you tried to do:

def with_price(price: float, factor: float = 2.0):
    return (price * factor)
    

def a_method(a: float, price_func):
    b = price_func(11)
    return a * b
    

print(a_method(5, with_price))

This calls a_method, with a set to 5 and price_func set to with_price. So, it computes b as 11 * 2.0, which is 22 and then returns 5 * 22, which is 110.

Note that doing it like this will cause factor to always be 2.0 since you provide no way of passing another value.

Also, you named things 'methods', but they are really just functions. Methods are functions defined on an object, which expect the first parameter to be a reference to the object they are being called on. You could turn them into methods by adding something like self as the first parameter and including them in the body of a class.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Many thanks for this explanation. My conclusion is that a method is defined within a class like ``` class Testme: def __init__(self, starting): self.starting = starting print(f"""\n{starting}\n""") def callme(self): print("Hello") ``` here callme() is a method of class Testme? on the other hand a function is just any function like abs(-3) etc that can be called anywhere – Mich Talebzadeh Dec 31 '21 at 00:37
  • 2
    You're correct - and the reason to collect functions as methods in a class is usually because they share some data in the object, which helps avoid defining globals (which have their own problems). You can define and reference variables on the object with `self.var_name` and they will be available in the methods under that name (because `self` is passed as the object itself. – Grismar Dec 31 '21 at 00:52
1

You can pass a method as argument to another method, but in your example you are creating an argument named with_price, which is unrelated to your function. You probably want to use with_price as a default argument for a_method. In my example I'll call the new argument func.

def with_price(price: float, factor: float = 2.0):
    return (price * factor)
    
def a_method(a: float, func=with_price):
    b = func(11)
    return a * b
print(a_method(1))
Franz Forstmayr
  • 1,219
  • 1
  • 15
  • 31
  • Hi Franz is there any difference between an argument and parameter or they are just synonyms in this case (can be used interchangeably) ? Thanks – Mich Talebzadeh Dec 31 '21 at 00:56
  • 1
    Just synonyms in this case. Here's a thread regarding this issue: https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter – Franz Forstmayr Jan 02 '22 at 17:16
1

In your snippet you are printing the function object a_method.

Passing a function as a parameter to another function is possible, but you need to actually call it if you want it to execute.

Considering your example:

  1. Define a function with_price
def with_price(price: float, factor: float = 2.0):
        return (price * factor)
  1. Define the function a_method. Note how myfunction is a generic parameter, not the function we just defined:
def a_method(a: float, myfunction):
        b = myfunction(11)
        return a * b
  1. Finally, call a_method, passing with_price as parameter:
print( a_method(2, with_price) )