Extend python function using code in another file

Lets say I have this:

def hello():
    print("HIYA")

And I want to add this to the function:

print("everyone")

How could I accomplish extending the function code WITHOUT entering the file and manually typing it out?, a bit like how you can add routes from other files using flask-blueprints.

FYI- modules are an option, any other ideas?

1 Like

I’ll refer you to the fundamental theorem of software engineering:

If you’re dead set on not solving this by literally opening that file and adding that line, then you can solve the problem by, instead of directly calling hello, call it ‘indirectly’ through a different function:

# suppose `that_file.py` contains the `hello` defined in the question
import that_file

def indirect_hello():
    that_file.hello()
    # you can add stuff
    print("everyone")

# no longer call `hello` directly
#that_file.hello()
indirect_hello()

But do think about the costs in terms of complexity and the maintenance costs of updating all references to the original hello. But hey, updating all the references is something you only have to do once; then after that, you can make changes to indirect_hello almost for free.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.