Python Forum
How do I make functions "interact" with each other?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make functions "interact" with each other?
#11
(Sep-06-2024, 02:49 PM)deanhystad Wrote: your post

@deanhystad

Arigato gozaimus.

Does LEGB refer to variable/function names/values and their accessibility. So ...
L (local) would refer to a variable and its value as computed by a particular function that's sequestered/isolated away from the rest of the program. (Vide infra)
def addition(a, b):
  total = a + b
  return total
addition(2, 3)
print(total)
In the above program (adding 2 numbers), the print(total) command would return an error because total is outside its scope? Is it correct to say that the a given variable is outside the scope of a command/function.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
G (global) variables are those that are "outside" all function calls and their value can be accessed by the entire program, si? Vide infra

weight_apples = 3
weight_bananas = 4
heavier = max(weight_apples, weight_bananas)

def price_total(weight_apples, weight_bananas):
   price_apples = weight_apples * 5
   price_bananas = weight_bananas * 4
   amount = price_apples + price_bananas
   return amount

print(price_total(weight_apples, weight_bananas))
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
E (Enclosed) variables (and their values) is something I don't understand. Are we talking about nested functions (vide infra)?

compute_cost(compute_volume(length, width, breadth))
Think
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
B (built in) refers to Python-reserved names like maxand min and print and for, correct? We can't use them as variable names.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Seeking clarification on the above.
Reply
#12
Python reserved names are words like "for", "while", "in". They are part of the language definition, and the interpreter will not accept these as variable names.
for = 4
Error:
for = 4 ^ SyntaxError: invalid syntax
Built-ins are functions that are automatically defined for any python program. You don't have to import a module to use built-in functions. min, max and print are built-in functions. You can use these as variable names, but doing so prevents you from using those functions.
def func(max, min):
    print(max - min)
    max = max(max, min)
    min = min(max, min)

func(0, 10)
Error:
-10 Traceback (most recent call last): File "test.py", line 6, in <module> func(0, 10) File "test.py", line 3, in range max = max(max, min) ^^^^^^^^^^^^^ TypeError: 'int' object is not callable
Using min and max as argument names to func() reassigns max = 0 and min = 10. Trying to call max() returns an error, because max references 0, not the built-in function.

An enclosed function is a function defined inside the scope of another. Like this:
def outer_function(variable):
    def inner_function():
        print(variable)

    inner(function)

function(5)
The inner_function() is only defined in the scope of the outer_function. It cannot be called outside the scope of that function. Variables that are local to the outer_function() are "enclosed" to the inner_function().

In this example, an enclosed function is used to create a "closure".
def power(y):

    def inner(x):
        return x**y
    
    return inner


squares = power(2)
cubes = power(3)
for i in range(1, 5):
    print(squares(i), cubes(i))
Output:
1 1 4 8 9 27 16 64
squares and cubes are closures that let us call inner() with different values for the enclosed variable "y". When calling squares(x), y == 2. When calling cubes(x), y == 3. The value of y is "enclosed" in the closure.
Hudjefa likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to make built-in functions available to my module functions? Pedroski55 1 933 Oct-28-2024, 06:40 AM
Last Post: Gribouillis
  How to make general functions for ecuations Mondata 6 3,951 Mar-11-2021, 09:15 PM
Last Post: Mondata
  cant able to make methods interact with each other in the class jagasrik 2 3,103 Sep-16-2020, 06:52 PM
Last Post: deanhystad
  Interact with Python Mario456 1 2,214 Jan-31-2020, 07:14 PM
Last Post: Larz60+
  How to open and interact with an app SheeppOSU 1 2,477 Feb-17-2019, 05:06 PM
Last Post: Larz60+
  How do you make functions that take a variable that is not defined? magic 6 5,937 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Functions to make an image zoom in until a response is made sawilliams 1 2,714 Aug-02-2018, 08:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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