# Merging multiple def()

**URL:** <https://discuss.python.org/t/merging-multiple-def/34592>\
**Category:** Python Help\
**Created:** [September 25, 2023, 5:20am UTC](https://discuss.python.org/t/merging-multiple-def/34592 "2023-09-25T05:20:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anno](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anno](https://discuss.python.org/u/Anno)\
**Post date:** [September 25, 2023, 5:20am UTC](https://discuss.python.org/t/merging-multiple-def/34592/1 "2023-09-25T05:20:10Z")

</div>

Hi all,

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [September 25, 2023, 5:26am UTC](https://discuss.python.org/t/merging-multiple-def/34592/2 "2023-09-25T05:26:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Anno](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anno](https://discuss.python.org/u/Anno)\
**Post date:** [September 25, 2023, 5:32am UTC](https://discuss.python.org/t/merging-multiple-def/34592/3 "2023-09-25T05:32:08Z")

</div>

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…

```auto
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']

```

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [September 25, 2023, 5:34am UTC](https://discuss.python.org/t/merging-multiple-def/34592/4 "2023-09-25T05:34:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anno](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anno](https://discuss.python.org/u/Anno)\
**Post date:** [September 25, 2023, 5:39am UTC](https://discuss.python.org/t/merging-multiple-def/34592/5 "2023-09-25T05:39:09Z")

</div>

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

I’ll be back later…

---

<div class="post-metadata">

**Author:** ![Anno](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anno](https://discuss.python.org/u/Anno)\
**Post date:** [September 25, 2023, 6:04am UTC](https://discuss.python.org/t/merging-multiple-def/34592/6 "2023-09-25T06:04:22Z")

</div>

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?

```auto
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)

```

* * *

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 25, 2023, 6:33am UTC](https://discuss.python.org/t/merging-multiple-def/34592/7 "2023-09-25T06:33:41Z")

</div>

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

> [@Anno](#):
>
> `x = int()`

This is a complex way to write:

```auto
x = 0

```

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

> [@Anno](#):
>
> ```auto
> def test(x,y):
>     
> for x in range(2):
> for y in range(2):
> print('Hello World!')            
> 
> ```

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:

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

```

Does this make sense?

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [September 25, 2023, 6:05pm UTC](https://discuss.python.org/t/merging-multiple-def/34592/8 "2023-09-25T18:05:02Z")

</div>

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

Perhaps with “merging” you mean “overloading”?

```auto
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:

```auto
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:

```auto
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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Anno](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anno](https://discuss.python.org/u/Anno)\
**Post date:** [October 10, 2023, 2:04am UTC](https://discuss.python.org/t/merging-multiple-def/34592/9 "2023-10-10T02:04:11Z")

</div>

@barry-scott @hansgeunsmeyer

Do you agree?

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

* * *

> [@barry-scott](#):
>
> 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?

```auto
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?

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [October 10, 2023, 2:33am UTC](https://discuss.python.org/t/merging-multiple-def/34592/10 "2023-10-10T02:33:16Z")

</div>

> [@Anno](#):
>
> x for x in…---------- is a set() ;  
> for x in…----------- is undeclared ‘set()’

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.
