Python Forum

Full Version: How do I make functions "interact" with each other?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(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.
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.
Pages: 1 2