Suppose I have a class called Cube and I create three objects:
class Cube():
def __init__(self, volume):
self.volume = volume
a = Cube(param_a)
b = Cube(param_b)
c = Cube(param_c)
I was wondering if I can write a function, to which the user can give a format, and the function can apply the format to the objects? For example:
>>> print_all(dummy_cube.volume)
a.volume
b.volume
c.volume
So basically the function contains a loop which will replace dummy_cube to a, b, c. I know I can use something like getattr(), but it has limits. For example, I hope that the function can print whatever format the user gives:
>>> print_all( (dummy_cube.volume)**2 + 1 )
(a.volume)**2 + 1
(b.volume)**2 + 1
(c.volume)**2 + 1
Is there any good way to do this?
Edit: As the comments pointed out, yes the function can take a list, and I intend it to return the numerical values instead of the format itself. With a list I can do like:
cube_list = [a, b, c]
for cube in cube_list:
print( (cube.volume)**2 + 1 )
But I am still not sure how I can do it with arbitrary expressions given by the user.