Skip to content Skip to sidebar Skip to footer

Access The Same Attribute From Different Objects In Python

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 = C

Solution 1:

The user can supply a function.

def print_all(f):
    for cube in cube_list:
        print(f(cube))

import operator

print_all(operator.attrgetter('volume'))
print_all(lambda c: c.volume**2 + 1)

Post a Comment for "Access The Same Attribute From Different Objects In Python"