Search code examples
pythonfunctionreturn-value

Is it possible to assign a variable to a function's (with an argument) return value without passing argument?


Example is especially simplified for explaining purposes. I am trying to get better any techniques or suggestions will be appreciated.

enter image description here

in run i am passing i variable to util.file_path and after i wanted to access in visualize.py in another function so i can assing it to sav_img then i wanna pass it to plt.savefig


Solution

  • It sounds like you want a setter/getter sort of thing. Rather than defining a function I might suggest just using a simple container like a dataclass:

    from dataclasses import dataclass
    
    @dataclass
    class MyData:
        file_path: str
    
    my_data = MyData("qwe")
    
    variable = my_data.file_path  # variable is now set to "qwe"
    my_data.file_path = "qwoo"
    variable = my_data.file_path  # variable is now set to "qwoo"