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ā¦

``````from typing import overload

def test(x: int, y: int):
...   # this is the Ellipsis object written as "..."
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

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