Python Forum
Why Pass Functions as arguments?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why Pass Functions as arguments?
#11
I do get it now. The flexibility of the code below. I can pass "any" function that has two parameters. Instead of changing the function arguments. It's just a little difficult to grasp at first. I'm not used to doing it this way.

This is a little off topic but, I think Python needs to restructure their namespacing. Like Javascript. Global, function and block. Just my 2 cents.

(Dec-01-2020, 01:50 PM)muzikman Wrote: Thank you for your example. Yes, I understand it.
If I were to call these functions by name from the calcit function, I would have to change the code in that function. Instead of just assigning the new function outside the main function. Thank you everyone for your help.

(Nov-30-2020, 11:47 PM)jefsummers Wrote: It's a level of flexibility.

def multiply(a,b):
    return a*b

def addit(a,b):
    return a+b

def calcit(func,op1,op2):
    return func(op1,op2)

function = multiply
a = 5
b = 6
print(calcit(function,a,b))
function = addit
print(calcit(function,a,b))
Output:
30 11
Reply
#12
(Jan-03-2021, 12:02 PM)muzikman Wrote: In other languages, you would pass a function as an argument like so, using brackets around the parameters:

This again is wrong. You aren't passing the function as the argument in that case, but the value that the function returns. The two example pieces of code you gave do different things - the latter does pass the function.
Reply
#13
(Jan-03-2021, 02:13 PM)muzikman Wrote: I think Python needs to restructure their namespacing. Like Javascript. Global, function and block.

I really don't understand people that come from some other language and want to change things like they are used to and don't care about backward compatibility and the existing code.

Check python scope and namespaces https://docs.python.org/3/tutorial/class...namespaces
Python has so called LEGB rule for scope - local, enclosing, global and built-in.
From the docs:
Quote:Although scopes are determined statically, they are used dynamically. At any time during execution, there are 3 or 4 nested scopes whose namespaces are directly accessible:
  • the innermost scope, which is searched first, contains the local names
  • the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
  • the next-to-last scope contains the current module’s global names
  • the outermost scope (searched last) is the namespace containing built-in names
JavaScript has global and local, while block scope is sort of "special" case when using certain keywords.

So basically it's just the block scope that you miss. In both cases it's always the most narrow possible scope being used (i.e. least access)

just my 2 cents....
DeaD_EyE likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
I didn't mean to rock the boat, I apologize. As I have mentioned before, learning Python has been a challenge. Some of the features are insanely powerful like List Comprehension. The simplicity of making splicing uniform across multiple data types.

my_string = [1: : 2]
my_list = [4:2:-1]
my_dict = [ : : -1]
etc.......

That is awesome!

Everywhere I go, people tell me to try and avoid using global and nonlocal variables. I am trying to do that. If you want access to a variable declared in the outter function from the inner, just pass it.

I remember how to use bitwise operators and Python makes it a snap to add, multiply binary.
int(5, base=whatever)
is awesome.

I love the fact that you can just type in any number format at the Python prompt: 0xff, 0o4, etc...

My point is, every language has it's pros and cons. Python is here to stay. It is so versatile. You can even build IOS or Android apps if you like.

Anyway, I am starting to really enjoy it. It would definitely be easier if I never learned how to code to begin with.

I do have some issues with it:

1. It lacks "true" abstraction and encapsulation. You cannot create private methods or variables. There are no interfaces, you can't define a class as abstract.

The reason for OOP is basically just to organize code better and sharing classes without the person knowing how the class is written but only how to use it. True encapsulation.

I don't understand all of these magic methods. I never did with PHP or any other scripting language that uses them.
Reply
#15
By using functions as arguments you can make abstractions or generalizations that you can use in a number of situations.
Consider as a "toy example" for sums:
1. the sum of integers from 1 to 10 (1 + 2 + ... + 10) = 55

2. the sum of squares from 1 to 10 (1 + 4 + ... + 100) = 385

3. the sum of the 10 first harmonic numbers = 2.928968...

4. the sum of odd numbers less than 10 (1 + 3 + ... + 9) = 25

if we generalize all this we get a pattern that can be described as
"the sum from a to b of a term taking a certain step". In the first
three cases the step is 1 and in the fourth it is 2.

In python this can be described as:
def sum(a, b, term, step):
    result = 0
    while a <= b:
        result += term(a)
        a = step(a)
    return result

# I also add the step functions
def step(y):
    return y+1

def step_2(y):
    return y+2
Take the sum of integers from 1 to 10:
print(sum(1, 10, lambda x: x, step)) #I need a function, why not a lambda
55
It might seem too much with a lambda but wait...

Take the sum of the squares of the first 10 integers
print(sum(1, 10, lambda x: x*x, step))
385
Take the sum of the 10 first harmonic numbers
print(sum(1, 10, lambda x: 1/x, step))
2.9289682539682538
Take the sum of the odd numbers less than 10
print(sum(1, 10, lambda x: x, step_2))
25
But we can make more complicated calculations even with this simple generalization, if we consider all kinds of summation of series of numbers, e.g. pi.

pi/8 can be described as the infinite sum from zero and up of 1 / ((4*x+1)*(4*x+3)). We should get a rough approximation with 1000000 terms:

def pi_term(x):
    return 1 / ((4*x+1)*(4*x+3))

print(8 * sum(0.0, 1000000, pi_term, step))
3.141592153590402
or the series 1/1 + 1/4 + 1/9 ... that gives us pi2/8
print(sqrt(8 * sum(1.0, 1000000, lambda x: 1/(x*x), step_2)))
3.1415920169700393
As we can see generalizations of this kind allows us to use patterns as functions that we specialize with other functions as arguments.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing writable arguments to functions. Assembler 11 923 Jan-15-2024, 11:32 PM
Last Post: sgrey
  How to pass encrypted pass to pyodbc script tester_V 0 852 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Passing string functions as arguments Clunk_Head 3 1,244 Jun-15-2022, 06:00 AM
Last Post: Gribouillis
  Possible to dynamically pass arguments to a function? grimm1111 2 2,169 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  how to pass arguments between pythons scripts? electricDesire 2 2,156 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,551 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to pass multiple arguments into function Mekala 4 2,446 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Pass Arguments to Function phillyfa 2 2,020 Mar-27-2020, 12:05 PM
Last Post: phillyfa
  Pass by reference vs Pass by value leodavinci1990 1 2,198 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  How to pass arguments to popen? zBernie 3 12,834 Jul-09-2018, 01:26 AM
Last Post: zBernie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020