Merging multiple def()

Hi all,

Q: How would multiple def() be merged? For example:

x = int()
y = int()

def test(x,y):
    
    for x in range(2):
        for y in range(2):
            print('Hello World!')            
# test(x,y)

def test1(x,y):
    for x in range(2):
        for y in range(2):
            print('The world, hello!')
# test1(x,y)

Thanks.

What do you mean by “merged”? What should happen when you use the “merged” resulting function, and how does that relate to what the original functions do?

Two def()s share the variables x and y. I’d like to simplify the script while achieving the same result.

Edit:

…been testing this type of thing…

x = int()
y = int()

def test(x,y):
    
    for x in range(2):
        for y in range(2):
            print('Hello World!')            
# test(x,y)

def test1(x,y):
    for x in range(2):
        for y in range(2):
            print('The world, hello!')
# test1(x,y)

keys = dict(var1 = test(x,y), var2 = test1(x,y))
keys['var1']

Just to make sure: you understand that right now, the code will completely ignore whatever is passed when you call the functions? And also that functions don’t share local variables (which includes parameters), ever? From Python’s perspective, the fact that both functions use the names x and y is completely coincidental and meaningless.

Yes. That’s the definition of function() in a more fundamental way.

I’ll be back later…

Is this the reason?

a,b,c,d …=> z ‘OK’
z = a,b,c,d ‘NOT ok’
a => z ‘OK’

… this example violates the function’s characteristics, thus they cannot be combined or shortened, correct?

x = int()
y = int()

def test(x,y):
    
    for x in range(2):
        for y in range(2):
            print('Hello World!')            
# test(x,y)

def test1(x,y):
for x in range(2):
for y in range(2):
            print('The world, hello!')
# test1(x,y)


In python it is not necessary to “declare” variables, as is done is some other languages.

This is a complex way to write:

x = 0

That is because int() is short for int(0) that is 0.

Here the x and y parameters are ignored, did you think you must declare x and y before you use them in the for loops?

You can write it as this:

def test():  
    for x in range(2):
        for y in range(2):
            print('Hello World!')            

Does this make sense?

2 Likes

Hello @Anno - I have to say I have never seen anyone tinkering with Python the way you do…

Perhaps with “merging” you mean “overloading”?

from typing import overload

@overload
def test(x: int, y: int):
     ...   # this is the Ellipsis object written as "..."
@overload
def test(x: str, y: str):
    ....
def test(x, y):
    if isinstance(x, int) and isinstance(y, int):
        print(f"Hello world from two numbers who are now tightly merged: {x + y}")
    elif isinstance(x, str) and isinstance(y, str):
        print(f"Hello world from the strings `{x}` and `{y}` who are still separate")
    else:
        raise ValueError("Sorry, I can only test strings or ints")

But here you still need to write your own, single ‘test’ function that does all the work.

If you start from two given functions, test1 and test2, the only way to “merge” them (that I can think of) is to write yet another function, say either_test1_or_test2 that will call either test1 or test2 depending on whatever conditions are relevant:

def either_test1_or_test2(x, y):
    if x > 0 and y > 0:
        return test1(x, y)
    else:
        return test2(x, y)

There is another possible way of “merging”. Perhaps you had this in mind?
You could call a function that takes another function as argument:

def test(func, *args):
    return func(*args)  # this is too simplistic too make practical sense, 
    # but is a pattern used for instance in functools.partial 

Now you would have:

test(int, 42) == 42
test(str, 42) == "42"

test(test, int, 42) == 42
test(test, test, test, str, 42) == "42"
test(*(test, test, test, int, 42)) == 42
1 Like

@barry-scott @hansgeunsmeyer

Do you agree?

x for x in…---------- is a set() ;
for x in…----------- is undeclared ‘set()’


def test():  
    for x in range(2):
        for y in range(2):
            print('Hello World!')

hmm…
Do you call it a function? To me, it’s a set of {value1, value2,…}

So,

  1. “def test()” is a function with the name “test” but no variables.

thinking…

So,

…test() and test(x,y) are simply different ways of writing the function. When using return(), perhaps the format should be something like test(value1, value2, value3,…) ----> Is it true?

No. I’m sorry. In all respect, if I may make a suggestion, I would say: Buy a book. Any Python beginner’s book is probably fine. Work through that, then come back with more questions. You need to first acquire a more basic understanding of programming in general. You cannot really get that in this forum, imo.

1 Like