MyFunction(f, g, n) is a bad function.
1. The name is meaningless.
2. The argument names are meaningless.
3. It makes your code harder to read than not using a function.
4. The function has no purpose that is easily described in a few short sentences.
5. What it does do is silly.
When you write a function it should have a well defined purpose. Let's say the purpose of your function is: Write a function "table" to a file. This would be my first attempt:
def write_function_table(function, values, filename):
with open(filename, "w") as file:
for value in values:
print(value, function(value), file=file)
write_function_table(lambda x: x**2, range(1, 11), "squares.txt")
This produces a file that looks like this:
Output:
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
It would be nice if the file "looked pretty", so I add a format string.
def write_function_table(function, values, filename, fmt_str="{inp}, {out}"):
with open(filename, "w") as file:
for value in values:
print(fmt_str.format(inp=value, out=function(value)), file=file)
write_function_table(lambda x: x**2, range(1, 11), "squares.txt", "square {inp} = {out}")
This produces:
Output:
square 1 = 1
square 2 = 4
square 3 = 9
square 4 = 16
square 5 = 25
square 6 = 36
square 7 = 49
square 8 = 64
square 9 = 81
square 10 = 100
It would be nice if I could use this function and have it write to stdout instead of a file, or maybe write multiple tables to one file.
def write_function_table(function, values, fmt_str="{inp}, {out}", file=None):
for value in values:
print(fmt_str.format(inp=value, out=function(value)), file=file)
values = range(1, 11)
with open("function tables.txt", "w") as file:
print("Squares", file=file)
write_function_table(lambda x: x**2, values, "{inp} = {out}", file)
print("\nSquare roots", file=file)
write_function_table(lambda x: x**0.5, values, "{inp} = {out}", file)
write_function_table(lambda x: x**3, values, "{inp}**3 = {out}")
This creates a function tables.txt file that looks like this:
Output:
Squares
1 = 1
2 = 4
3 = 9
4 = 16
5 = 25
6 = 36
7 = 49
8 = 64
9 = 81
10 = 100
Square roots
1 = 1.0
2 = 1.4142135623730951
3 = 1.7320508075688772
4 = 2.0
5 = 2.23606797749979
6 = 2.449489742783178
7 = 2.6457513110645907
8 = 2.8284271247461903
9 = 3.0
10 = 3.1622776601683795
And also writes this to stdout.
Output:
1**3 = 1
2**3 = 8
3**3 = 27
4**3 = 64
5**3 = 125
6**3 = 216
7**3 = 343
8**3 = 512
9**3 = 729
10**3 = 1000
And to make it easier for others to use my function I would provide documentation.
def write_function_table(function, values, fmt_str="{x}, {fx}", file=None):
"""Write a "function table" to the file.
function : The function to evaluate for x
values : Iterable of x values
fmt_str : String compatible with format() command. Use "x" for x and "fx" for function(x)
file : File to write results. If None, writes to stdout
"""
for x in values:
print(fmt_str.format(x=x, fx=function(x)), file=file)
values = range(1, 11)
with open("squares.txt", "w") as file:
write_function_table(lambda x: x**2, values, "square {x} = {fx}", file)
write_function_table(lambda x: x**3, values, "{x}**3 = {fx}")
Now not only can I use it, but others can use it too. And when my users ask me to add generating square root tables or combining multiple tables in one file it will be easy for me to quickly modify my program to add those capabilities.