0
Follow
0
View

Pythonic way to reuse code blocks in functions

qq287153236 注册会员
2023-01-25 16:56

Put the common code in another function that returns the values.

def common(path, x, y):
    img = load_img(path)
    img.normalize
    return img, Foo(x), Bar(y)

def func1(path, x, y):
    img, foo, bar = common(path, x, y)

    # different code

def func2(path, x, y):
    img, foo, bar = common(path, x, y)

    # different code

You might also consider using a decorator.

About the Author

Question Info

Publish Time
2023-01-25 16:56
Update Time
2023-01-25 16:56